package io.bdmc.core.msp2000;

import io.bdmc.common.utils.ByteUtil;
import io.bdmc.common.utils.StringUtils;
import io.bdmc.core.dts.DtsFullData;
import io.bdmc.core.msp2000.model.*;
import io.bdmc.core.snmp.GwUtil;
import io.bdmc.modules.bss.entity.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 设备上行数据解析 设备->网管
 */
public class DataParse {

    /**
     * 解析消息头
     * 
     * @param bs bs
     * @return Msp2000Ter2Platform
     */
    public static Msp2000Ter2Platform parseAllMsg(byte[] bs) {
        Msp2000Ter2Platform msp = new Msp2000Ter2Platform();

        msp.setFlag(ByteUtil.bytesToHex(ByteUtil.copyByte(3, bs, 0)));
        msp.setLen(ByteUtil.twoBytesToInteger(ByteUtil.copyByte(2, bs, 3)));
        msp.setVer(Integer.parseInt(Objects.requireNonNull(ByteUtil.bytesToHex(ByteUtil.copyByte(1, bs, 5)))));
        msp.setMflag(bs[6]);
        if (msp.getMflag() == 0) {
            // 单帧
            int count = 0;
            if (msp.getVer() > 11) {
                count = msp.getVer() - 11;
                msp.setRes(ByteUtil.bytesToHex(ByteUtil.copyByte(count, bs, 7)));
            }
            msp.setComm(ByteUtil.bytesToHex(ByteUtil.copyByte(2, bs, 7 + count)));
            msp.setTcomm(ByteUtil.twoBytesToInteger(ByteUtil.copyByte(2, bs, 9 + count)));
            msp.setHsum(ByteUtil.bytesToHex(ByteUtil.copyByte(1, bs, 11 + count)));
            msp.setData(ByteUtil.bytesToHex(ByteUtil.copyByte(msp.getLen() - 8, bs, 12 + count)));
        } else {
            // 多帧
            msp.setAmount(ByteUtil.twoBytesToInteger(ByteUtil.copyByte(2, bs, 7)));
            msp.setNo(ByteUtil.twoBytesToInteger(ByteUtil.copyByte(2, bs, 9)));
            int count = 0;
            if (msp.getVer() > 15) {
                count = msp.getVer() - 15;
                msp.setRes(ByteUtil.bytesToHex(ByteUtil.copyByte(count, bs, 11)));
            }
            msp.setComm(ByteUtil.bytesToHex(ByteUtil.copyByte(2, bs, 11 + count)));
            msp.setTcomm(ByteUtil.twoBytesToInteger(ByteUtil.copyByte(2, bs, 13 + count)));
            msp.setHsum(ByteUtil.byteToHex(bs[15 + count]));
            msp.setData(ByteUtil.bytesToHex(ByteUtil.copyByte(msp.getLen() - 12 - count, bs, 16 + count)));
        }
        msp.setSum(ByteUtil.byteToHex(bs[msp.getLen() + 5 - 1]));
        return msp;
    }

    public static DeviceMachineFrame parseDeviceMachineFrame(String data) {
        DeviceMachineFrame df = new DeviceMachineFrame();
        MainFrame mf = parseMainFrame(data);
        df.setProductType(mf.productType);
        df.setFrameSn(mf.sn);
        df.setProtocolVersion(mf.protlVer);
        df.setDataType(ByteUtil.HexToInt(mf.dataType));
        df.setLocation(mf.locationInfo);
        df.setRacksCount(mf.racksCount);
        df.setCreateTime(LocalDateTime.now());
        df.setUpdateTime(LocalDateTime.now());
        return df;

    }

    private static MainFrame parseMainFrame(String data) {
        MainFrame mainframe = new MainFrame();
        mainframe.productType = data.substring(0, 6);
        mainframe.sn = data.substring(6, 18);
        mainframe.protlVer = "V" + ByteUtil.HexToInt(data.substring(18, 20)) / 10.0;
        mainframe.dataType = data.substring(20, 22);
        mainframe.locationInfo = ByteUtil.HexToStr(data.substring(22, 22 + 64)).trim();
        mainframe.racksCount = ByteUtil.HexToInt(data, 22 + 64, 2);
        // data = data.substring(88);
        // for (int i = 0; i < mainframe.racksCount ; i++) {
        // RacksData rData = parseRacksData(data);
        // data = data.substring(48);
        // }
        // SlotData sData = parseSlotData(data);
        // data = data.substring(48);
        // FanPlate f = parseFanPlate(data);

        return mainframe;
    }

    public static DeviceMachineRack parseDeviceMachineRack(String data) {
        DeviceMachineRack rack = new DeviceMachineRack();
        RacksData rd = parseRacksData(data);
        rack.setRackNum(rd.racksNum + "");
        rack.setProductType(rd.productType);
        rack.setRackSn(rd.sn);
        rack.setProtocolVersion(rd.protlVer);
        rack.setSoftwareVersion(rd.platVersion.substring(0, 6));
        rack.setHardwareVersion(rd.platVersion.substring(6, 8));
        rack.setSlotNumber(rd.slotCount);
        rack.setStatus(rd.systemStatus);
        rack.setPlatform(rd.platformInfo + "");
        rack.setCreateTime(LocalDateTime.now());
        rack.setRackCount(rd.slotCount);
        return rack;
    }

    private static RacksData parseRacksData(String data) {
        RacksData racksData = new RacksData();
        racksData.racksNum = ByteUtil.HexToInt(data, 0, 2);
        racksData.length = ByteUtil.HexToInt(data, 2, 4);
        racksData.productType = data.substring(6, 12);
        racksData.sn = data.substring(12, 12 + 12);
        racksData.protlVer = "V" + ByteUtil.HexToInt(data.substring(24, 24 + 2)) / 10.0;
        racksData.platVersion = data.substring(26, 26 + 8);
        racksData.slotNumber = ByteUtil.HexToInt(data, 34, 2);
        racksData.slotCount = ByteUtil.HexToInt(data, 36, 2);
        racksData.systemStatus = ByteUtil.HexToInt(data, 38, 2);
        racksData.platformInfo = ByteUtil.HexToInt(data, 40, 2);
        racksData.frameTemperature = ByteUtil.HexToInt(data, 42, 4) / 10.0;
        racksData.res = data.substring(46, 46 + 2);

        return racksData;
    }

    public static DeviceMachineDisk parseDeviceMachineDisk(SlotData sd) {
        DeviceMachineDisk disk = new DeviceMachineDisk();

        disk.setSlotPort(sd.getSlotPort());
        disk.setDataValidity(sd.getDataValidity());
        disk.setSlotSversion(sd.getSltSVer());
        disk.setSlotHversion(sd.getSlotHVer());
        disk.setProtocolVersion(sd.getProtlVer());
        disk.setModuleNum(sd.getMdNumber());
        disk.setModuleCount(sd.getModuleCount());
        disk.setModuleData("");
        disk.setTemper(ByteUtil.doubleToString(sd.getTemper()));
        disk.setSlotHw(sd.getSlotHW() + "");
        disk.setLocation(sd.getLocation());
        disk.setCreateTime(LocalDateTime.now());
        disk.setUpdateTime(LocalDateTime.now());
        disk.setDiskSn(sd.getSn());
        return disk;
    }

    public static SlotData parseSlotData(String data) {

        SlotData slotData = new SlotData();
        slotData.setSlotPort(ByteUtil.HexToInt(data, 0, 2));
        slotData.setLength(ByteUtil.HexToInt(data, 2, 4));
        slotData.setSn(data.substring(6, 6 + 12));
        slotData.setType(slotData.getSn().substring(3, 6));

        slotData.setDataValidity(data.substring(18, 18 + 2));
        slotData.setSltSVer("V" + ByteUtil.HexToInt(data.substring(20, 20 + 2)) / 10.0);
        slotData.setSlotHVer("V" + ByteUtil.HexToInt(data, 22, 2) / 10.0);
        slotData.setProtlVer("V" + ByteUtil.HexToInt(data, 24, 2) / 10.0);
        slotData.setMdNumber(ByteUtil.HexToInt(data, 26, 2));
        slotData.setModuleCount(ByteUtil.HexToInt(data, 28, 2));
        slotData.setRes1(ByteUtil.HexToInt(data, 30, 2));
        slotData.setSlotHW(ByteUtil.HexToInt(data, 32, 2));
        slotData.setTemper(ByteUtil.HexToInt(data, 34, 4) / 10.0);
        if (!data.substring(38, 38 + 64).contains("FFFFFF")) {
            slotData.setLocation(ByteUtil.HexToStr(data.substring(38, 38 + 64)).replace(" ", ""));
        }

        if (slotData.getType().equals("330")) {
            if (slotData.getLength() < 60) {
                slotData.setLength(60);
            }
            slotData.setBody(data.substring(102, 102 + slotData.getLength() * 2));
            slotData.setAllLen(102 + slotData.getLength() * 2);

        } else {
            slotData.setResLen(data.substring(102, 104));
            slotData.setBody(data.substring(104, 104 + slotData.getLength() * 2));
            slotData.setAllLen(104 + slotData.getLength() * 2);
        }

        return slotData;
    }

    public static FanPlate parseFanPlate(String data) {

        FanPlate fanPlate = new FanPlate();

        fanPlate.fans = new ArrayList<>();
        fanPlate.powers = new ArrayList<>();

        fanPlate.fanNum = ByteUtil.HexToInt(data, 0, 2);
        for (int i = 0; i < fanPlate.fanNum; i++) {
            String fanData = data.substring(2 + i * 32, 2 + i * 32 + 32);
            DeviceMachineFan fan = parseFan(fanData);
            fan.setFanName("风扇" + (i + 1));
            fan.setFanNum(i + 1);
            fanPlate.fans.add(fan);
        }
        fanPlate.powerNum = ByteUtil.HexToInt(data, 2 + fanPlate.fanNum * 32, 2);
        int startLen = 4 + fanPlate.fanNum * 32;

        for (int i = 0; i < fanPlate.powerNum; i++) {
            String powerData = data.substring(startLen + i * 8, startLen + i * 8 + 8);
            fanPlate.powers.add(parsePower(powerData));
        }
        startLen += fanPlate.powerNum * 8;
        fanPlate.temperatureAlarm = ByteUtil.HexToInt(data, startLen, 2);
        fanPlate.temperatureAlarmThreshold = ByteUtil.HexToInt(data, startLen + 2, 2) - 127;
        fanPlate.temperatureAlarmHysteresis = ByteUtil.HexToInt(data, startLen + 4, 2);
        return fanPlate;
    }

    private static DeviceMachineFan parseFan(String data) {
        DeviceMachineFan fan = new DeviceMachineFan();
        fan.setFanMode(ByteUtil.HexToInt(data, 0, 2));
        int statusI = ByteUtil.HexToInt(data, 2, 2);
        String status = "";
        if (statusI == 0) {
            status = "在位";
        }
        if (statusI == 1) {
            status = "不在位";
        }

        if (statusI == 2) {
            status = "损坏";
        }
        if (statusI == 4) {
            status = "达到使用寿命";
        }
        fan.setFanStatus(status);
        fan.setFanRpm(ByteUtil.HexToInt(data, 4, 4));
        fan.setFanMaxRpm(ByteUtil.HexToInt(data, 8, 4));
        fan.setFanMinRpm(ByteUtil.HexToInt(data, 12, 4));
        fan.setFanLife(ByteUtil.HexToInt(data, 16, 8));
        fan.setRunTime(ByteUtil.HexToInt(data, 24, 8));
        fan.setCreateTime(LocalDateTime.now());
        return fan;
    }

    private static DeviceMachinePower parsePower(String data) {
        DeviceMachinePower power = new DeviceMachinePower();
        String tmp = data.substring(0, 2);
        String type = "";
        switch (tmp) {
            case "00":
                type = "不存在";
                break;
            case "01":
                type = "48V";
                break;
            case "02":
                type = "220V";
                break;
        }
        power.setPowerType(type);

        String tmp1 = data.substring(2, 4);
        String location = "";
        switch (tmp1) {
            case "00":
                location = "不存在";
                break;
            case "01":
                location = "后置电源1";
                break;
            case "02":
                location = "后置电源2";
                break;
            case "11":
                location = "前置电源1";
                break;
            case "12":
                location = "前置电源2";
                break;
            case "13":
                location = "前置电源3";
                break;
        }

        power.setPowerLocation(location);

        if (type.equals("不存在")) {
            power.setPowerVol("-");
        } else {
            power.setPowerVol(ByteUtil.HexToInt(data, 4, 4) / 10.0 + "");
        }

        power.setCreateTime(LocalDateTime.now());
        return power;
    }

    public static List<DeviceMachineFsp> parseDeviceMachineFspOLPS(String thresholdDatas,String currentDatas) {
        List<DeviceMachineFsp> lt = new ArrayList<>();
        String deviceSn = thresholdDatas.substring(thresholdDatas.length()-8,thresholdDatas.length()-2);
        for (int i = 0; i < 8; i++) {
            if(thresholdDatas.length()>=58 && currentDatas.length()>=58){
                String thresholdData = thresholdDatas.substring(0,58);
                String currentData = currentDatas.substring(0,58);
                thresholdDatas = thresholdDatas.substring(58);
                currentDatas = currentDatas.substring(58);
                DeviceMachineFsp fsp = parseDeviceMachineFspOLP(thresholdData,currentData);
                fsp.setDeviceSN(deviceSn);
                lt.add(fsp);
            }
        }
        return lt;
    }


    /**
     * 新增OLP协议解析
     * 相当于之前1U的设备
     * @param thresholdData 阈值
     * @param currentData 当前值
     * @return DeviceMachineFsp
     */
    public static DeviceMachineFsp parseDeviceMachineFspOLP(String thresholdData,String currentData) {
        DeviceMachineFsp fsp = new DeviceMachineFsp();
      
        int slotNumber = ByteUtil.HexToInt(thresholdData, 0, 2);
        fsp.setSingleSlot(slotNumber);
        double txAlarm = ((byte) ByteUtil.HexToInt(thresholdData, 2, 2) * 256 + ByteUtil.HexToInt(thresholdData, 4, 2)) / 10.0;
        double t1Alarm = ((byte) ByteUtil.HexToInt(thresholdData, 6, 2) * 256 + ByteUtil.HexToInt(thresholdData, 8, 2)) / 10.0;
        double t2Alarm = ((byte) ByteUtil.HexToInt(thresholdData, 10, 2) * 256 + ByteUtil.HexToInt(thresholdData, 12, 2)) / 10.0;

        double rxAlarm = ((byte) ByteUtil.HexToInt(thresholdData, 14, 2) * 256 + ByteUtil.HexToInt(thresholdData, 16, 2)) / 10.0;
        double r1Alarm = ((byte) ByteUtil.HexToInt(thresholdData, 18, 2) * 256 + ByteUtil.HexToInt(thresholdData, 20, 2)) / 10.0;
        double r2Alarm = ((byte) ByteUtil.HexToInt(thresholdData, 22, 2) * 256 + ByteUtil.HexToInt(thresholdData, 24, 2)) / 10.0;

        double r1Switch = ((byte) ByteUtil.HexToInt(thresholdData, 26, 2) * 256 + ByteUtil.HexToInt(thresholdData, 28, 2)) / 10.0;
        double r2Switch = ((byte) ByteUtil.HexToInt(thresholdData, 30, 2) * 256 + ByteUtil.HexToInt(thresholdData, 32, 2)) / 10.0;


      
        double backTime = ByteUtil.HexToInt(thresholdData, 50, 4) / 10.0;



        String workType = currentData.substring(2,4);
        String lineType = currentData.substring(4, 6);
        int switchingDelay = ByteUtil.HexToInt(currentData, 6, 2) * 4;

        String  workingChannel = currentData.substring(8, 10);
        switch (workingChannel) {
            case "F3":
                workingChannel = "主发主收";
                break;
            case "F2":
                workingChannel = "主发从收";
                break;
            case "F1":
                workingChannel = "从发主收";
                break;
            case "F0":
                workingChannel = "从发从收";
                break;
        }
        String alarmInfo = currentData.substring(10, 12);
     
        double tx = ((byte) ByteUtil.HexToInt(currentData, 12, 2) * 256 + ByteUtil.HexToInt(currentData, 14, 2)) / 10.0;
        double t1 = ((byte) ByteUtil.HexToInt(currentData, 16, 2) * 256 + ByteUtil.HexToInt(currentData, 18, 2)) / 10.0;
        double t2 = ((byte) ByteUtil.HexToInt(currentData, 20, 2) * 256 + ByteUtil.HexToInt(currentData, 22, 2)) / 10.0;

        double rx = ((byte) ByteUtil.HexToInt(currentData, 24, 2) * 256 + ByteUtil.HexToInt(currentData, 26, 2)) / 10.0;
        double r1 = ((byte) ByteUtil.HexToInt(currentData, 28, 2) * 256 + ByteUtil.HexToInt(currentData, 30, 2)) / 10.0;
        double r2 = ((byte) ByteUtil.HexToInt(currentData, 32, 2) * 256 + ByteUtil.HexToInt(currentData, 34, 2)) / 10.0;
        String sn = thresholdData.substring(42,48);
        lineType = lineType.toUpperCase();
        fsp.setWorkMode(workType);
        fsp.setSwitchDelay(switchingDelay + "");
        fsp.setWorkingChannel(workingChannel);
        fsp.setLineType(lineType);
        fsp.setAlarmInfo(alarmInfo);
        fsp.setTxPower(ByteUtil.doubleToString(tx));
        fsp.setT1Power(ByteUtil.doubleToString(t1));
        fsp.setT2Power(ByteUtil.doubleToString(t2));
        fsp.setRxPower(ByteUtil.doubleToString(rx));
        fsp.setR1Power(ByteUtil.doubleToString(r1));
        fsp.setR2Power(ByteUtil.doubleToString(r2));
        fsp.setDiskSn(sn);
        fsp.setTxAlarmThreshold(ByteUtil.doubleToString(txAlarm));
        fsp.setT1AlarmThreshold(ByteUtil.doubleToString(t1Alarm));
        fsp.setT2AlarmThreshold(ByteUtil.doubleToString(t2Alarm));
        fsp.setRxAlarmThreshold(ByteUtil.doubleToString(rxAlarm));
        fsp.setR1AlarmThreshold(ByteUtil.doubleToString(r1Alarm));
        fsp.setR2AlarmThreshold(ByteUtil.doubleToString(r2Alarm));
        fsp.setR1SwitchThreshold(ByteUtil.doubleToString(r1Switch));
        fsp.setR2SwitchThreshold(ByteUtil.doubleToString(r2Switch));
        fsp.setReturnTime(backTime + "");
        fsp.setCreateTime(LocalDateTime.now());
    
        return fsp;
    }
    public static DeviceMachineFsp parseDeviceMachineFsp(String data) {
        DeviceMachineFsp fsp = new DeviceMachineFsp();
        FSP f = parseFSP(data);

        fsp.setSingleSlot(f.slotNumber);
        String workMode = f.operatingMode2;
        // switch (f.operatingMode2) {
        // case "10":
        // workMode = "自动不返回(单纤)";
        // break;
        // case "20":
        // workMode = "自动返回(单纤)";
        // break;
        // case "30":
        // workMode = "自动不返回(双纤)";
        // break;
        // case "40":
        // workMode = "自动返回(双纤)";
        // break;
        // case "50":
        // workMode = "手动";
        // break;
        // case "60":
        // workMode = "强制";
        // break;
        // case "70":
        // workMode = "主线路";
        // break;
        // case "80":
        // workMode = "备线路";
        // break;
        //
        // }

        String workingChannel = "";
        switch (f.workingChannel) {
            case "F3":
                workingChannel = "主发主收";
                break;
            case "F2":
                workingChannel = "主发从收";
                break;
            case "F1":
                workingChannel = "从发主收";
                break;
            case "F0":
                workingChannel = "从发从收";
                break;
        }

        fsp.setWorkMode(workMode);
        fsp.setSwitchDelay(f.switchingDelay + "");
        fsp.setWorkingChannel(workingChannel);

        String lineType = f.lineType.toUpperCase();
        // switch (f.lineType.toUpperCase()) {
        // case "15":
        // lineType = "主备点对点";
        // break;
        // case "14":
        // lineType = "主点对点,备OEO";
        // break;
        // case "10":
        // lineType = "主备含OEO";
        // break;
        // case "1F":
        // lineType = "主备含EDFA";
        // break;
        // }

        fsp.setLineType(lineType);
        fsp.setAlarmInfo(f.alarmInfo);
        fsp.setTxPower(ByteUtil.doubleToString(f.tx));
        fsp.setT1Power(ByteUtil.doubleToString(f.t1));
        fsp.setT2Power(ByteUtil.doubleToString(f.t2));
        fsp.setRxPower(ByteUtil.doubleToString(f.rx));
        fsp.setR1Power(ByteUtil.doubleToString(f.r1));
        fsp.setR2Power(ByteUtil.doubleToString(f.r2));
        fsp.setSerial(f.sn);
        fsp.setSoftwareVersion(f.softVersion);
        fsp.setTxAlarmThreshold(ByteUtil.doubleToString(f.txAlarm));
        fsp.setT1AlarmThreshold(ByteUtil.doubleToString(f.t1Alarm));
        fsp.setT2AlarmThreshold(ByteUtil.doubleToString(f.t2Alarm));
        fsp.setRxAlarmThreshold(ByteUtil.doubleToString(f.rxAlarm));
        fsp.setR1AlarmThreshold(ByteUtil.doubleToString(f.r1Alarm));
        fsp.setR2AlarmThreshold(ByteUtil.doubleToString(f.r2Alarm));
        fsp.setR1SwitchThreshold(ByteUtil.doubleToString(f.r1Switch));
        fsp.setR2SwitchThreshold(ByteUtil.doubleToString(f.r2Switch));
        fsp.setLightSwitchThreshold(ByteUtil.doubleToString(f.lightSwitchThreshold));
        fsp.setPatternMismatch(f.patternMismatchFlag);
        fsp.setReturnTime(f.backTime + "");
        fsp.setStateAlarm(f.getStateAlarm());
        fsp.setCreateTime(LocalDateTime.now());

        return fsp;
    }

    private static FSP parseFSP(String data) {

        FSP fsp = new FSP();

        fsp.slotNumber = ByteUtil.HexToInt(data, 0, 2);
        fsp.operatingMode1 = data.substring(2, 4);
        fsp.operatingMode2 = data.substring(4, 6);
        fsp.lineType = data.substring(6, 8);
        fsp.switchingDelay = ByteUtil.HexToInt(data, 8, 2) * 4;
        fsp.workingChannel = data.substring(10, 12);
        fsp.alarmInfo = data.substring(12, 14);
        fsp.tx = ((byte) ByteUtil.HexToInt(data, 14, 2) * 256 + ByteUtil.HexToInt(data, 16, 2)) / 10.0;
        fsp.t1 = ((byte) ByteUtil.HexToInt(data, 18, 2) * 256 + ByteUtil.HexToInt(data, 20, 2)) / 10.0;
        fsp.t2 = ((byte) ByteUtil.HexToInt(data, 22, 2) * 256 + ByteUtil.HexToInt(data, 24, 2)) / 10.0;

        fsp.rx = ((byte) ByteUtil.HexToInt(data, 26, 2) * 256 + ByteUtil.HexToInt(data, 28, 2)) / 10.0;
        fsp.r1 = ((byte) ByteUtil.HexToInt(data, 30, 2) * 256 + ByteUtil.HexToInt(data, 32, 2)) / 10.0;
        fsp.r2 = ((byte) ByteUtil.HexToInt(data, 34, 2) * 256 + ByteUtil.HexToInt(data, 36, 2)) / 10.0;

        fsp.tt = ((byte) ByteUtil.HexToInt(data, 38, 2) * 256 + ByteUtil.HexToInt(data, 40, 2)) / 10.0;
        fsp.rr = ((byte) ByteUtil.HexToInt(data, 42, 2) * 256 + ByteUtil.HexToInt(data, 44, 2)) / 10.0;

        fsp.sn = data.substring(46, 52);
        fsp.extra = data.substring(52, 54);
     
        String hex = ByteUtil.hexString2binaryString(data.substring(54, 56));
        if(hex.substring(0,1).equals("1")){
              fsp.stateAlarm +="7,";
        }
         if(hex.substring(1,2).equals("1")){
              fsp.stateAlarm +="6,";
        }
        if(hex.substring(2,3).equals("1")){
              fsp.stateAlarm +="5,";
        }
        if(hex.substring(3,4).equals("1")){
              fsp.stateAlarm +="4,";
        }
        if(hex.substring(4,5).equals("1")){
              fsp.stateAlarm +="3,";
        }

        fsp.softVersion = "V" + ByteUtil.HexToInt(data, 56, 2) / 10.0;
        fsp.extra1 = data.substring(58, 60);
        fsp.checksum1 = data.substring(60, 62);

        fsp.txAlarm = ((byte) ByteUtil.HexToInt(data, 62, 2) * 256 + ByteUtil.HexToInt(data, 64, 2)) / 10.0;
        fsp.t1Alarm = ((byte) ByteUtil.HexToInt(data, 66, 2) * 256 + ByteUtil.HexToInt(data, 68, 2)) / 10.0;
        fsp.t2Alarm = ((byte) ByteUtil.HexToInt(data, 70, 2) * 256 + ByteUtil.HexToInt(data, 72, 2)) / 10.0;

        fsp.rxAlarm = ((byte) ByteUtil.HexToInt(data, 74, 2) * 256 + ByteUtil.HexToInt(data, 76, 2)) / 10.0;
        fsp.r1Alarm = ((byte) ByteUtil.HexToInt(data, 78, 2) * 256 + ByteUtil.HexToInt(data, 80, 2)) / 10.0;
        fsp.r2Alarm = ((byte) ByteUtil.HexToInt(data, 82, 2) * 256 + ByteUtil.HexToInt(data, 84, 2)) / 10.0;

        fsp.r1Switch = ((byte) ByteUtil.HexToInt(data, 86, 2) * 256 + ByteUtil.HexToInt(data, 88, 2)) / 10.0;
        fsp.r2Switch = ((byte) ByteUtil.HexToInt(data, 90, 2) * 256 + ByteUtil.HexToInt(data, 92, 2)) / 10.0;
        fsp.ttAlarm = ((byte) ByteUtil.HexToInt(data, 94, 2) * 256 + ByteUtil.HexToInt(data, 96, 2)) / 10.0;

        fsp.lightSwitchThreshold = ((byte) ByteUtil.HexToInt(data, 98, 2) * 256 + ByteUtil.HexToInt(data, 100, 2))
                / 10.0;
        fsp.sn1 = data.substring(102, 102 + 6);
        fsp.patternMismatchFlag = data.substring(108, 110);
        fsp.backTime = ByteUtil.HexToInt(data, 110, 4) / 10.0;
        // fsp.extra2 = data.substring(114, 114+4);
        // fsp.checksum2 = data.substring(118, 118+2);
        return fsp;
    }

    public static DeviceMachineEdfa parseDeviceMachineEdfa(String data) {
        EDFA e = parseEDFA(data);
        DeviceMachineEdfa edfa = new DeviceMachineEdfa();
        edfa.setAlarmState1(e.alarmState);
        edfa.setAlarmState2(e.alarmState1);
        String type = e.moduleType.substring(0, 2);
        // if (e.moduleType.substring(0, 2).equals("00")) {
        // type = "单波";
        // } else if (e.moduleType.substring(0, 2).equals("01")) {
        // type = "窄带";
        // } else if (e.moduleType.substring(0, 2).equals("10")) {
        // type = "C带";
        // } else if (e.moduleType.substring(0, 2).equals("11")) {
        // type = "C+";
        // }

        String lineLocation = e.moduleType.substring(2, 4);
        // if (e.moduleType.substring(2, 4).equals("00")) {
        // lineLocation = "无标识";
        // } else if (e.moduleType.substring(2, 4).equals("01")) {
        // lineLocation = "BA";
        // } else if (e.moduleType.substring(2, 4).equals("10")) {
        // lineLocation = "LA";
        // } else if (e.moduleType.substring(2, 4).equals("11")) {
        // lineLocation = "PA";
        // }
        edfa.setLineLocation(lineLocation);

        edfa.setModuleType(type);
        edfa.setPumpsCount(e.pumpsNum);
        edfa.setLightPathSeries(e.opticalPathLevel + "");

        String workModel = e.workModel.substring(1, 4);
        // if (e.workModel.substring(1, 4).equals("000")) {
        // workModel = "Disable";
        // } else if (e.workModel.substring(1, 4).equals("001")) {
        // workModel = "AGC";
        // } else if (e.workModel.substring(1, 4).equals("010")) {
        // workModel = "APC";
        // } else if (e.workModel.substring(1, 4).equals("011")) {
        // workModel = "ACC";
        // } else if (e.workModel.substring(1, 4).equals("100")) {
        // workModel = "总功率AGC";
        // } else if (e.workModel.substring(1, 4).equals("101")) {
        // workModel = "APPC";
        // } else if (e.workModel.substring(1, 4).equals("110")) {
        // workModel = "MP";
        // }
        edfa.setWorkMode(workModel);

        edfa.setApcPowerSetting(ByteUtil.doubleToString(e.singleStageAPCPowerSettings));

        String workState = e.workState.charAt(3) + e.workState.substring(6, 7);
        // if (e.workState.substring(3, 4).equals("1")) {
        // workState = "无光关泵状态";
        // } else if (e.workState.substring(6, 7).equals("1")) {
        // workState = "关泵状态";
        // }

        edfa.setWorkStatus(workState);
        edfa.setMinPower(ByteUtil.doubleToString(e.minPower));
        edfa.setRatedPower(ByteUtil.doubleToString(e.ratedPower));
        edfa.setOutputPower(ByteUtil.doubleToString(e.singleStageOutputPower));
        edfa.setInputPower(ByteUtil.doubleToString(e.singleStageInputPower));
        edfa.setTemper(ByteUtil.doubleToString(e.temper));
        edfa.setTemperMax(ByteUtil.doubleToString(e.temperMax));
        edfa.setTemperMin(ByteUtil.doubleToString(e.temperMin));
        edfa.setInputLowGate(ByteUtil.doubleToString(e.SingleInputLowThreshold));
        edfa.setInputHighGate(ByteUtil.doubleToString(e.SingleInputHighThreshold));
        edfa.setOutputAlarmGate(ByteUtil.doubleToString(e.singleStageOutputAlarmThreshold));
        edfa.setInputLightlessAlarmThreshold(ByteUtil.doubleToString(e.singleStageInputNoLightAlarmThreshold));
        edfa.setPump1Current(ByteUtil.doubleToString(e.pump1Currents));
        edfa.setPump1Power(ByteUtil.doubleToString(e.pump1Power));
        edfa.setPump1Temper(ByteUtil.doubleToString(e.tubeCoreTemperature1));
        edfa.setRefdCurrent(ByteUtil.doubleToString(e.refrigerationCurrent1));
        edfa.setCreateTime(LocalDateTime.now());
        edfa.setUpdateTime(LocalDateTime.now());
        return edfa;
    }

    public static EDFA parseEDFA(String data) {

        EDFA edfa = new EDFA();

        edfa.readAll = data.substring(0, 16);
        edfa.length = ByteUtil.HexToInt(data, 16, 4);
        edfa.version = "V" + ByteUtil.HexToInt(data, 20, 2) / 10.0;
        edfa.McuFwVer = data.substring(22, 24);
        edfa.FpgaFwVer = data.substring(24, 26);
        edfa.moduleType = ByteUtil.hexString2binaryString(data.substring(26, 28));
        edfa.wavelengthChannelNum = ByteUtil.HexToShort(data, 28, 4);
        edfa.minGain = ByteUtil.HexToShort(data, 32, 4) / 10.0;
        edfa.maxGain = ByteUtil.HexToShort(data, 36, 4) / 10.0;
        edfa.minPower = ByteUtil.HexToShort(data, 40, 4) / 10.0;
        edfa.ratedPower = ByteUtil.HexToShort(data, 44, 4) / 10.0;

        edfa.pumpsNum = ByteUtil.HexToShort(data, 48, 2);
        edfa.opticalPathLevel = ByteUtil.HexToShort(data, 50, 2);
        edfa.voaNum = ByteUtil.HexToShort(data, 52, 2);

        edfa.moduleSn = data.substring(54, 60);
        edfa.workModel = ByteUtil.hexString2binaryString(data.substring(60, 62));
        edfa.alarmModel = ByteUtil.hexString2binaryString(data.substring(62, 66));

        edfa.singleStageAPCPowerSettings = ByteUtil.HexToShort(data, 82, 4) / 10.0;

        edfa.workState = ByteUtil.hexString2binaryString(data.substring(118, 124));
        edfa.alarmState = ByteUtil.hexString2binaryString(data.substring(124, 130));
        edfa.pump1Currents = ByteUtil.HexToShort(data, 130, 4) / 10.0;
        edfa.pump1Power = ByteUtil.HexToShort(data, 134, 4) / 10.0;
        edfa.tubeCoreTemperature1 = ByteUtil.HexToShort(data, 138, 4) / 10.0;
        edfa.refrigerationCurrent1 = ByteUtil.HexToShort(data, 142, 4) / 10.0;
        edfa.pump2Currents = ByteUtil.HexToShort(data, 146, 4) / 10.0;
        edfa.pump2Power = ByteUtil.HexToShort(data, 150, 4) / 10.0;
        edfa.tubeCoreTemperature2 = ByteUtil.HexToShort(data, 154, 4) / 10.0;
        edfa.refrigerationCurrent2 = ByteUtil.HexToShort(data, 158, 4) / 10.0;

        // 单级输入功率
        edfa.singleStageInputPower = ByteUtil.HexToShort(data, 162, 4) / 10.0;

        // 单级输出功率
        edfa.singleStageOutputPower = ByteUtil.HexToShort(data, 166, 4) / 10.0;

        edfa.temper = ByteUtil.HexToShort(data, 186, 4) / 10.0;

        // 单级输入无光告警门限
        edfa.singleStageInputNoLightAlarmThreshold = ByteUtil.HexToShort(data, 202, 4) / 10.0;

        // 单级输入低门限
        edfa.SingleInputLowThreshold = ByteUtil.HexToShort(data, 206, 4) / 10.0;

        // 单级输入高门限
        edfa.SingleInputHighThreshold = ByteUtil.HexToShort(data, 210, 4) / 10.0;

        // 单级输出告警门限
        edfa.singleStageOutputAlarmThreshold = ByteUtil.HexToShort(data, 214, 4) / 10.0;

        edfa.temperMin = ByteUtil.HexToShort(data, 234, 4) / 10.0;
        edfa.temperMax = ByteUtil.HexToShort(data, 238, 4) / 10.0;

        // 告警状态4-6字节
        edfa.alarmState1 = ByteUtil.hexString2binaryString(data.substring(338, 344));

        return edfa;
    }

    public static DeviceMachineFec parseDeviceMachineFec(String data, String type) {
        DeviceMachineFec fec = new DeviceMachineFec();
        List<DeviceMachineFecModule> deviceMachineFecModuleList = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        if (type.equals("2.5")) {
            FEC25G f = parseFEC25G(data);
            fec.setWorkMode(f.fecData.WorkMode + "");
            fec.setSpeedCodeClient(ByteUtil.HexToInt(f.fecData.SpeedCode, 0, 2) + "");
            fec.setSpeedCode(ByteUtil.HexToInt(f.fecData.SpeedCode, 2, 2) + "");
            fec.setStates(ByteUtil.HexToInt(f.fecData.States, 0, 2) + "");
            fec.setCodeGain(ByteUtil.HexToInt(f.fecData.States, 2, 2) + "");
            String hardwareLoopStates = ByteUtil.hexString2binaryString(f.fecData.LoopStates.substring(0, 2))
                    .substring(4);
            String chipLoopStates = ByteUtil.hexString2binaryString(f.fecData.LoopStates.substring(2, 4)).substring(4);

            fec.setLoopStates("0");

            fec.setBit0("0");
            fec.setBit1("0");
            fec.setBit2("0");
            fec.setBit3("0");
            if (!hardwareLoopStates.equals("0000")) {
                fec.setLoopStates("1");
                fec.setBit0(String.valueOf(hardwareLoopStates.charAt(3)));
                fec.setBit1(String.valueOf(hardwareLoopStates.charAt(2)));
                fec.setBit2(String.valueOf(hardwareLoopStates.charAt(1)));
                fec.setBit3(String.valueOf(hardwareLoopStates.charAt(0)));
            }
            if (!chipLoopStates.equals("0000")) {
                fec.setLoopStates("2");
                fec.setBit0(String.valueOf(chipLoopStates.charAt(3)));
                fec.setBit1(String.valueOf(chipLoopStates.charAt(2)));
                fec.setBit2(String.valueOf(chipLoopStates.charAt(1)));
                fec.setBit3(String.valueOf(chipLoopStates.charAt(0)));
            }

            fec.setAlarm(f.fecData.Alarm);
            // int uchRecdStatus = ByteUtil.HexToInt(f.fecData.fifteenMinutes1.substring(0,
            // 2));
            // int uchErrStatus = ByteUtil.HexToInt(f.fecData.fifteenMinutes1.substring(2,
            // 4));
            // int ushErrRecdIndex =
            // ByteUtil.HexToInt(f.fecData.fifteenMinutes1.substring(4, 8));
            long uiRecdStartSecnd = ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(8, 16));
            // long uiErrStartSecnd =
            // ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(16, 24));
            long uiRecdEndSecnd = ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(24, 32));

            fec.setRxM1NowError(ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(32, 40)) + "");
            fec.setRxB2NowError(ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(40, 48)) + "");
            fec.setRxB1NowError(ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(48, 56)) + "");
            fec.setTxM1NowError(ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(56, 64)) + "");
            fec.setTxB2NowError(ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(64, 72)) + "");
            fec.setTxB1NowError(ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(72, 80)) + "");

            fec.setUiBip8ErrCnt(ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(80, 88)) + "");
            fec.setUiBeiErrCnt(ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(88, 96)) + "");
            fec.setUi1ErrCnt(ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(96, 104)) + "");
            fec.setUi0ErrCnt(ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(104, 112)) + "");
            fec.setUiUncrtBlockCnt(ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(112, 120)) + "");
            fec.setUiRxBlocksCnt(ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(120, 128)) + "");

            fec.setUshRxB1Es(ByteUtil.HexToInt(f.fecData.fifteenMinutes1.substring(128, 132)) + "");
            fec.setUshRxB1Ses(ByteUtil.HexToInt(f.fecData.fifteenMinutes1.substring(132, 136)) + "");
            fec.setUshRxB1Uas(ByteUtil.HexToInt(f.fecData.fifteenMinutes1.substring(136, 140)) + "");
            fec.setUshTxB1Es(ByteUtil.HexToInt(f.fecData.fifteenMinutes1.substring(140, 144)) + "");
            fec.setUshTxB1Ses(ByteUtil.HexToInt(f.fecData.fifteenMinutes1.substring(144, 148)) + "");
            fec.setUshTxB1Uas(ByteUtil.HexToInt(f.fecData.fifteenMinutes1.substring(148, 152)) + "");

            fec.setUshTxB1As((uiRecdEndSecnd - uiRecdStartSecnd - Integer.parseInt(fec.getUshTxB1Uas()) + 1) + "");
            fec.setUshRxB1As((uiRecdEndSecnd - uiRecdStartSecnd - Integer.parseInt(fec.getUshRxB1Uas()) + 1) + "");

            double ufRxBer = (Double.parseDouble(fec.getUi1ErrCnt()) + Long.parseLong(fec.getUi0ErrCnt())
                    + Long.parseLong(fec.getUiUncrtBlockCnt()))
                    / ((Long.parseLong(fec.getUshTxB1As()) + Long.parseLong(fec.getUshRxB1As())) * 10709225316L);
            fec.setUfRxBer(ByteUtil.doubleToString(ufRxBer));
            double ubRxBer = Double.parseDouble(fec.getUiBeiErrCnt())
                    / (Long.parseLong(fec.getUshTxB1As()) + Long.parseLong(fec.getUshRxB1As()) * 10709225316L);
            fec.setUbRxBer(ByteUtil.doubleToString(ubRxBer));

            fec.setCreateTime(now);

            for (FEC25GModel m : f.models) {
                DeviceMachineFecModule fm = new DeviceMachineFecModule();
                fm.setDdm(m.DDMAlarm);
                if (m.Mode.charAt(6) == '0') {
                    fm.setWorkMode("单向转发");
                } else {
                    fm.setWorkMode("双向转发");
                }
                if (m.Mode.charAt(1) == '0') {
                    fm.setAuthState("认证成功");
                } else {
                    fm.setAuthState("未认证");
                }

                if (m.Mode.startsWith("00", 2)) {
                    fm.setAuthMode("认证关闭");
                }
                if (m.Mode.startsWith("01", 2)) {
                    fm.setAuthMode("强制认证");
                }
                if (m.Mode.startsWith("10", 2)) {
                    fm.setAuthMode("提示认证");
                }
                fm.setLaserMode(m.Mode.substring(4, 6));
                // if (m.Mode.startsWith("00",4)) {
                // fm.setLaserMode("无光不关闭");
                // } else {
                // fm.setLaserMode("无光将关闭");
                // }

                int index = f.models.indexOf(m) + 1;
                fm.setModuleName("模块" + index);

                if (m.Wavelength == 0) {
                    fm.setStates("不在位");
                } else {
                    fm.setStates("在位");
                }
                fm.setDescription(ByteUtil.doubleToString(m.Wavelength) + ",2.5G");

                fm.setTemper(ByteUtil.doubleToString(m.temper));
                fm.setBiasCurrent(ByteUtil.doubleToString(m.TxBias));
                fm.setTxPower(ByteUtil.doubleToString(m.TxPower));
                fm.setRxPower(ByteUtil.doubleToString(m.RxPower));
                fm.setWaveLength(ByteUtil.doubleToString(m.Wavelength * 10));
                fm.setTemperMin(ByteUtil.doubleToString(m.temperatureLow));
                fm.setTemperMax(ByteUtil.doubleToString(m.temperatureUpper));
                fm.setBiasCurrentMin(ByteUtil.doubleToString(m.TxBiasLow));
                fm.setBiasCurrentMax(ByteUtil.doubleToString(m.TxBiasUpper));
                fm.setTransmitPowerMin(ByteUtil.doubleToString(m.TXPowerLow));
                fm.setTransmitPowerMax(ByteUtil.doubleToString(m.TXPowerUpper));
                fm.setReceivedPowerMax(ByteUtil.doubleToString(m.RxPowerUpper));
                fm.setReceivedPowerMin(ByteUtil.doubleToString(m.RxPowerLow));

                fm.setCreateTime(now);
                deviceMachineFecModuleList.add(fm);
            }
            fec.setFecModuleList(deviceMachineFecModuleList);
        } else {
            FEC10G f = parseFEC10G(data);
            fec.setWorkMode(f.getFecData().WorkMode + "");
            fec.setSpeedCodeClient(ByteUtil.HexToInt(f.getFecData().SpeedCode, 0, 2) + "");
            fec.setSpeedCode(ByteUtil.HexToInt(f.getFecData().SpeedCode, 2, 2) + "");

            fec.setStates(ByteUtil.HexToInt(f.getFecData().States, 0, 2) + "");
            fec.setCodeGain(ByteUtil.HexToInt(f.getFecData().States, 2, 2) + "");
            String hardwareLoopStates = ByteUtil.hexString2binaryString(f.getFecData().LoopStates.substring(0, 2))
                    .substring(4);
            String chipLoopStates = ByteUtil.hexString2binaryString(f.getFecData().LoopStates.substring(2, 4))
                    .substring(4);

            fec.setLoopStates("0");
            fec.setBit0("0");
            fec.setBit1("0");
            fec.setBit2("0");
            fec.setBit3("0");
            if (!hardwareLoopStates.equals("0000")) {
                fec.setLoopStates("1");
                fec.setBit0(String.valueOf(hardwareLoopStates.charAt(3)));
                fec.setBit1(String.valueOf(hardwareLoopStates.charAt(2)));
                fec.setBit2(String.valueOf(hardwareLoopStates.charAt(1)));
                fec.setBit3(String.valueOf(hardwareLoopStates.charAt(0)));
            }
            if (!chipLoopStates.equals("0000")) {
                fec.setLoopStates("2");
                fec.setBit0(String.valueOf(chipLoopStates.charAt(3)));
                fec.setBit1(String.valueOf(chipLoopStates.charAt(2)));
                fec.setBit2(String.valueOf(chipLoopStates.charAt(1)));
                fec.setBit3(String.valueOf(chipLoopStates.charAt(0)));
            }
            fec.setAlarm(f.getFecData().Alarm);
            fec.setRxM1NowError(ByteUtil.HexToLong(f.getFecData().fifteenMinutes1.substring(16, 24)) + "");
            fec.setRxB1NowError(ByteUtil.HexToLong(f.getFecData().fifteenMinutes1.substring(24, 32)) + "");
            fec.setRxB2NowError(ByteUtil.HexToLong(f.getFecData().fifteenMinutes1.substring(32, 40)) + "");
            fec.setTxM1NowError(ByteUtil.HexToLong(f.getFecData().fifteenMinutes1.substring(40, 48)) + "");
            fec.setTxB1NowError(ByteUtil.HexToLong(f.getFecData().fifteenMinutes1.substring(48, 56)) + "");
            fec.setTxB2NowError(ByteUtil.HexToLong(f.getFecData().fifteenMinutes1.substring(56, 64)) + "");
            fec.setCreateTime(now);

            for (FEC10GModel m : f.getModels()) {
                DeviceMachineFecModule fm = new DeviceMachineFecModule();
                fm.setDdm(m.getAlarm1());
                if (m.getMode().charAt(6) == '0') {
                    fm.setWorkMode("单向转发");
                } else {
                    fm.setWorkMode("双向转发");
                }
                if (m.getMode().charAt(1) == '0') {
                    fm.setAuthState("认证成功");
                } else {
                    fm.setAuthState("未认证");
                }

                if (m.getMode().startsWith("00", 2)) {
                    fm.setAuthMode("认证关闭");
                }
                if (m.getMode().startsWith("01", 2)) {
                    fm.setAuthMode("强制认证");
                }
                if (m.getMode().startsWith("10", 2)) {
                    fm.setAuthMode("提示认证");
                }
                fm.setLaserMode(m.getMode().substring(4, 6));
                // if (m.getMode().charAt(4) == '0') {
                // fm.setLaserMode("无光不关闭");
                // } else {
                // fm.setLaserMode("无光将关闭");
                // }

                int index = f.getModels().indexOf(m) + 1;
                fm.setModuleName("模块" + index);

                if (!m.getMode().contains("1")) {
                    fm.setStates("不在位");
                } else {
                    fm.setStates("在位");
                }
                fm.setDescription(ByteUtil.doubleToString(m.getLsWaveOff()) + ",10G");

                fm.setTemper(ByteUtil.doubleToString(m.getMdTemp()));
                fm.setBiasCurrent(ByteUtil.doubleToString(m.getTxBias()));
                fm.setTxPower(ByteUtil.doubleToString(m.getTxPower()));
                fm.setRxPower(ByteUtil.doubleToString(m.getRxPower()));
                fm.setWaveLength(ByteUtil.doubleToString(m.getLsCtWave() * 10));
                fm.setTemperMin(ByteUtil.doubleToString(m.getMdTempThsLow()));
                fm.setTemperMax(ByteUtil.doubleToString(m.getMdTempThsUp()));
                fm.setBiasCurrentMin(ByteUtil.doubleToString(m.getTxBiasThsLow()));
                fm.setBiasCurrentMax(ByteUtil.doubleToString(m.getTxBiasThsUp()));
                fm.setTransmitPowerMin(ByteUtil.doubleToString(m.getTxPowerThsLow()));
                fm.setTransmitPowerMax(ByteUtil.doubleToString(m.getTxPowerThsUp()));
                fm.setReceivedPowerMax(ByteUtil.doubleToString(m.getRxPowerThsUP()));
                fm.setReceivedPowerMin(ByteUtil.doubleToString(m.getRxPowerThsLow()));

                fm.setCreateTime(now);
                deviceMachineFecModuleList.add(fm);
            }
            fec.setFecModuleList(deviceMachineFecModuleList);
        }

        return fec;
    }

    private static FEC25G parseFEC25G(String data) {

        FEC25G fec = new FEC25G();

        List<FEC25GModel> models = new ArrayList<>();

        for (int i = 0; i < 2; i++) {
            FEC25GModel model = new FEC25GModel();
            model.Mode = ByteUtil.hexString2binaryString(data.substring(0, 2));
            model.Maddr = data.substring(2, 4);
            model.temper = ByteUtil.HexToInt(data, 4, 4) / 256.0;
            model.VCCVoltage = ByteUtil.HexToInt(data, 8, 4) / 10.0 / 1000.0;
            model.TxBias = ByteUtil.HexToInt(data, 12, 4) * 2 / 1000.0;
            model.TxPower = 10 * Math.log10(ByteUtil.HexToInt(data, 16, 4) / 10.0 / 1000.0);
            model.RxPower = 10 * Math.log10(ByteUtil.HexToInt(data, 20, 4) / 10.0 / 1000.0);
            model.DDMAlarm = data.substring(24, 28);
            model.RES = data.substring(28, 32);

            if (data.substring(32, 38).equalsIgnoreCase("FFFFFF")) {
                model.Wavelength = 0;
            } else {
                int tmp = ByteUtil.HexToInt(data, 36, 2);
                double d = tmp / Math.pow(10, (tmp + "").length());
                model.Wavelength = ByteUtil.HexToInt(data, 32, 4) + d;
            }

            model.temperatureLow = ByteUtil.HexToInt(data, 38, 4) / 256.0;

            model.temperatureUpper = ByteUtil.HexToInt(data, 42, 4) / 256.0;

            model.VoltageUpper = ByteUtil.HexToInt(data, 46, 4) / 10.0 / 1000.0;
            model.VoltageLow = ByteUtil.HexToInt(data, 50, 4) / 10.0 / 1000.0;

            model.TxBiasUpper = ByteUtil.HexToInt(data, 54, 4) * 2 / 1000.0;
            model.TxBiasLow = ByteUtil.HexToInt(data, 58, 4) * 2 / 1000.0;
            model.TXPowerUpper = 10 * Math.log10(ByteUtil.HexToInt(data, 62, 4) / 10.0 / 1000.0);
            model.TXPowerLow = 10 * Math.log10(ByteUtil.HexToInt(data, 66, 4) / 10.0 / 1000.0);

            model.RxPowerUpper = 10 * Math.log10(ByteUtil.HexToInt(data, 70, 4) / 10.0 / 1000.0);
            model.RxPowerLow = 10 * Math.log10(ByteUtil.HexToInt(data, 74, 4) / 10.0 / 1000.0);

            model.EbhancedOptions = data.substring(78, 88);
            models.add(model);
            data = data.substring(88);
        }
        fec.models = models;
        FECOther other = new FECOther();
        other.channel = ByteUtil.HexToInt(data, 0, 2);
        other.WorkMode = ByteUtil.HexToInt(data, 2, 2);
        other.SpeedCode = data.substring(4, 8);
        other.States = data.substring(8, 12);
        other.LoopStates = data.substring(12, 16);
        other.Alarm = data.substring(16, 24);
        other.RES = data.substring(24, 32);

        other.fifteenMinutes1 = data.substring(32, 184);
        other.uchRecdStatus = ByteUtil.HexToInt(other.fifteenMinutes1.substring(0, 2));
        other.uchErrStatus = ByteUtil.HexToInt(other.fifteenMinutes1.substring(2, 4));

        other.fifteenMinutes2 = data.substring(184, 336);
        fec.fecData = other;

        return fec;
    }

    private static FEC10G parseFEC10G(String data) {

        FEC10G fec = new FEC10G();

        List<FEC10GModel> models = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            FEC10GModel model = new FEC10GModel();
            model.setMode(ByteUtil.hexString2binaryString(data.substring(0, 2)));
            model.setMdAddr(data.substring(2, 4));
            model.setSigCtl(data.substring(4, 6));
            model.setAuxType(ByteUtil.hexString2binaryString(data.substring(6, 8)));
            model.setLsWaveSet(ByteUtil.HexToInt(data, 8, 4) * 0.005);
            model.setLsWaveOff((Integer.valueOf(data.substring(12, 16), 16).shortValue()) * 0.005);

            model.setMdTemp((Integer.valueOf(data.substring(16, 20), 16).shortValue()) / 256.0);
            model.setTxBias(ByteUtil.HexToInt(data, 20, 4) * 2 / 1000.0);

            model.setTxPower(10 * Math.log10(ByteUtil.HexToInt(data, 24, 4) / 10.0 / 1000.0));
            model.setRxPower(10 * Math.log10(ByteUtil.HexToInt(data, 28, 4) / 10.0 / 1000.0));
            model.setAuxValue11(data.substring(32, 36));
            model.setAuxValue12(data.substring(36, 40));

            model.setCtrlStatus(data.substring(40, 44));
            model.setAlarm1(data.substring(44, 54));
            model.setRES1(data.substring(54, 56));
            model.setExtIdntf(data.substring(56, 58));

            model.setDvTech(data.substring(58, 60));
            model.setCDR(data.substring(60, 62));
            model.setDMT(data.substring(62, 64));
            model.setEnhOpts(data.substring(64, 66));

            model.setRES2(data.substring(66, 68));
            model.setMdTempThsUp((Integer.valueOf(data.substring(68, 72), 16).shortValue()) / 256.0);
            model.setMdTempThsLow((Integer.valueOf(data.substring(72, 76), 16).shortValue()) / 256.0);

            model.setTxBiasThsUp(ByteUtil.HexToInt(data, 76, 4) * 2 / 1000.0);
            model.setTxBiasThsLow(ByteUtil.HexToInt(data, 80, 4) * 2 / 1000.0);
            model.setTxPowerThsUp(10 * Math.log10(ByteUtil.HexToInt(data, 84, 4) / 10.0 / 1000.0));
            model.setTxPowerThsLow(10 * Math.log10(ByteUtil.HexToInt(data, 88, 4) / 10.0 / 1000.0));
            model.setRxPowerThsUP(10 * Math.log10(ByteUtil.HexToInt(data, 92, 4) / 10.0 / 1000.0));
            model.setRxPowerThsLow(10 * Math.log10(ByteUtil.HexToInt(data, 96, 4) / 10.0 / 1000.0));
            model.setAuxThsUp1(data.substring(100, 104));
            model.setAuxThsLow1(data.substring(104, 108));
            model.setAuxThsUp2(data.substring(108, 112));
            model.setAuxThsLow2(data.substring(112, 116));
            model.setVPSCtrl(data.substring(116, 120));
            model.setBERRept(data.substring(120, 124));
            model.setFECCtrl(data.substring(124, 128));
            model.setLsCtWave(ByteUtil.HexToInt(data, 128, 4) * 0.005);
            model.setLsWaveTolerance(ByteUtil.HexToInt(data, 132, 4) * 0.005);
            model.setCoding(data.substring(136, 140));
            model.setExMdType(ByteUtil.hexString2binaryString(data.substring(140, 142)));
            model.setExDataLen(data.substring(142, 144));
            models.add(model);
            data = data.substring(144);
        }
        fec.setModels(models);
        FECOther other = new FECOther();
        other.channel = ByteUtil.HexToInt(data, 0, 2);
        other.WorkMode = ByteUtil.HexToInt(data, 2, 2);
        other.SpeedCode = data.substring(4, 8);
        other.States = data.substring(8, 12);
        other.LoopStates = data.substring(12, 16);
        other.Alarm = data.substring(16, 24);
        other.RES = data.substring(24, 32);
        other.fifteenMinutes1 = data.substring(32, 184);
        other.uchRecdStatus = ByteUtil.HexToInt(other.fifteenMinutes1.substring(0, 2));
        other.uchErrStatus = ByteUtil.HexToInt(other.fifteenMinutes1.substring(2, 4));

        other.fifteenMinutes2 = data.substring(184, 336);
        fec.setFecData(other);

        return fec;
    }

    public static DeviceMachineRfa parseDeviceMachineRfa(String data) {
        DeviceMachineRfa rfa = new DeviceMachineRfa();
        RFA r = parseRFA(data);

        rfa.setAlarmState1(r.alarmState);
        // rfa.setAlarmState2(r.alarmState1);
        String type = r.moduleType.substring(0, 2);
        // if (r.moduleType.substring(0, 2).equals("00")) {
        // type = "单波";
        // } else if (r.moduleType.substring(0, 2).equals("01")) {
        // type = "窄带";
        // } else if (r.moduleType.substring(0, 2).equals("10")) {
        // type = "C带";
        // } else if (r.moduleType.substring(0, 2).equals("11")) {
        // type = "C+";
        // }
        rfa.setModuleType(type);

        String lineLocation = r.moduleType.substring(2, 4);
        // if (r.moduleType.substring(2, 4).equals("00")) {
        // lineLocation = "无标识";
        // } else if (r.moduleType.substring(2, 4).equals("01")) {
        // lineLocation = "BA";
        // } else if (r.moduleType.substring(2, 4).equals("10")) {
        // lineLocation = "LA";
        // } else if (r.moduleType.substring(2, 4).equals("11")) {
        // lineLocation = "PA";
        // }
        rfa.setLineLocation(lineLocation);

        String workModel = r.workingMode.substring(1, 4);
        // if (r.workingMode.substring(1, 4).equals("000")) {
        // workModel = "Disable";
        // } else if (r.workingMode.substring(1, 4).equals("001")) {
        // workModel = "AGC";
        // } else if (r.workingMode.substring(1, 4).equals("010")) {
        // workModel = "APC";
        // } else if (r.workingMode.substring(1, 4).equals("011")) {
        // workModel = "ACC";
        // } else if (r.workingMode.substring(1, 4).equals("100")) {
        // workModel = "总功率AGC";
        // } else if (r.workingMode.substring(1, 4).equals("101")) {
        // workModel = "APPC";
        // } else if (r.workingMode.substring(1, 4).equals("110")) {
        // workModel = "MP";
        // }
        rfa.setWorkMode(workModel);

        String workState = r.workState.substring(3, 4);
        // if (r.workState.substring(3, 4).equals("1")) {
        // workState = "无光关泵状态";
        // } else if (r.workState.substring(6, 7).equals("1")) {
        // workState = "关泵状态";
        // }
        rfa.setWorkState(workState);
        rfa.setInputLightlessAlarmThreshold(ByteUtil.doubleToString(r.singleInputNoLightAlarmHysteresis));
        rfa.setWaveChannelCount(r.wavelengthChannelNum);
        rfa.setGainMin(ByteUtil.doubleToString(r.gainMin));
        rfa.setGainMax(ByteUtil.doubleToString(r.gainMax));
        rfa.setGainSetting(ByteUtil.doubleToString(r.singleStageGainSettings));
        rfa.setTemper(ByteUtil.doubleToString(r.boxTemperature));
        rfa.setActualGain(ByteUtil.doubleToString(r.singleStageModuleGainActualValue));
        rfa.setTemperMin(ByteUtil.doubleToString(r.moduleBoxTemperatureLowAlarmThreshold));
        rfa.setTemperMax(ByteUtil.doubleToString(r.moduleBoxTemperatureHighAlarmThreshold));
        rfa.setReflectiveAlarmThreshold(ByteUtil.doubleToString(r.reflectionAlarmThreshold));
        rfa.setReflectedPower(ByteUtil.doubleToString(r.reflectedPower));
        rfa.setOutputSignalPower(ByteUtil.doubleToString(r.ramanOutputSignalPower));
        rfa.setInputSignalPower(ByteUtil.doubleToString(r.ramanInputSignalPower));
        rfa.setPump1Current(ByteUtil.doubleToString(r.pump1Currents));
        rfa.setPump1Power(ByteUtil.doubleToString(r.pump1Power));
        rfa.setPump1Tepmer(ByteUtil.doubleToString(r.tubeCoreTemperature1));
        rfa.setPump1RefaCurrent(ByteUtil.doubleToString(r.refrigerationCurrent1));
        rfa.setPump2Current(ByteUtil.doubleToString(r.pump2Currents));
        rfa.setPump2Power(ByteUtil.doubleToString(r.pump2Power));
        rfa.setPump2Tepmer(ByteUtil.doubleToString(r.tubeCoreTemperature2));
        rfa.setPump2RefaCurrent(ByteUtil.doubleToString(r.refrigerationCurrent2));
        rfa.setCreateTime(LocalDateTime.now());

        return rfa;
    }

    private static RFA parseRFA(String data) {

        RFA rfa = new RFA();

        rfa.readAll = data.substring(0, 16);
        rfa.length = ByteUtil.HexToInt(data.substring(16, 20));
        rfa.moduleType = ByteUtil.hexString2binaryString(data.substring(26, 28));
        rfa.wavelengthChannelNum = ByteUtil.HexToInt(data, 28, 4);
        rfa.gainMin = ByteUtil.HexToInt(data, 32, 4) / 10.0;
        rfa.gainMax = ByteUtil.HexToInt(data, 36, 4) / 10.0;
        rfa.pumpCount = ByteUtil.HexToInt(data.substring(48, 50));
        rfa.opticalPathLevel = ByteUtil.HexToInt(data, 50, 2);
        rfa.voaCount = ByteUtil.HexToInt(data, 52, 2);
        rfa.moduleSn = data.substring(54, 60);
        rfa.workingMode = ByteUtil.hexString2binaryString(data.substring(60, 62));
        rfa.alarmMode = ByteUtil.hexString2binaryString(data.substring(62, 66));
        rfa.singleStageGainSettings = ByteUtil.HexToInt(data, 66, 4) / 10.0;
        rfa.gainSlope = data.substring(78, 82);
        rfa.workState = ByteUtil.hexString2binaryString(data.substring(82 + 36, 124));
        rfa.alarmState = ByteUtil.hexString2binaryString(data.substring(124, 130));
        rfa.pump1Currents = ByteUtil.HexToInt(data, 130, 4) / 10.0;
        rfa.pump1Power = ByteUtil.HexToInt(data, 134, 4) / 10.0;
        rfa.tubeCoreTemperature1 = ByteUtil.HexToInt(data, 138, 4) / 10.0;
        rfa.refrigerationCurrent1 = ByteUtil.HexToInt(data, 142, 4) / 10.0;
        rfa.pump2Currents = ByteUtil.HexToInt(data, 146, 4) / 10.0;
        rfa.pump2Power = ByteUtil.HexToInt(data, 150, 4) / 10.0;
        rfa.tubeCoreTemperature2 = ByteUtil.HexToInt(data, 154, 4) / 10.0;
        rfa.refrigerationCurrent2 = ByteUtil.HexToShort(data, 158, 4) / 10.0;
        rfa.singleInput = ByteUtil.HexToShort(data, 162, 4) / 10.0;
        rfa.singleOutput = ByteUtil.HexToShort(data, 166, 4) / 10.0;
        rfa.boxTemperature = ByteUtil.HexToInt(data, 186, 4) / 10.0;
        rfa.singleStageModuleGainActualValue = ByteUtil.HexToInt(data, 190, 4) / 10.0;
        rfa.singleInputLightlessWarningThreshold = ByteUtil.HexToShort(data, 202, 4) / 10.0;
        rfa.moduleBoxTemperatureLowAlarmThreshold = ByteUtil.HexToShort(data, 234, 4) / 10.0;
        rfa.moduleBoxTemperatureHighAlarmThreshold = ByteUtil.HexToInt(data, 238, 4) / 10.0;

        rfa.pumpCurrentWarningThreshold1 = ByteUtil.HexToInt(data, 246, 4) / 10.0;
        rfa.pumpPowerWarningThreshold1 = ByteUtil.HexToInt(data, 250, 4) / 10.0;
        rfa.pumpTemperatureWarningThreshold1 = ByteUtil.HexToInt(data, 254, 4) / 10.0;
        rfa.pumpCurrentWarningThreshold2 = ByteUtil.HexToInt(data, 258, 4) / 10.0;
        rfa.pumpPowerWarningThreshold2 = ByteUtil.HexToInt(data, 262, 4) / 10.0;
        rfa.pumpTemperatureWarningThreshold2 = ByteUtil.HexToInt(data, 266, 4) / 10.0;

        rfa.singleInputNoLightAlarmHysteresis = ByteUtil.HexToInt(data, 270, 4) / 10.0;
        rfa.moduleTemperatureLowAlarmHysteresis = ByteUtil.HexToInt(data, 302, 4) / 10.0;
        rfa.moduleTemperatureHighAlarmHysteresis = ByteUtil.HexToInt(data, 306, 4) / 10.0;

        rfa.pumpCurrentAlarmHysteresis1 = ByteUtil.HexToShort(data, 314, 4) / 10.0;
        rfa.pumpPowerAlarmHysteresis1 = ByteUtil.HexToShort(data, 318, 4) / 10.0;
        rfa.pumpTemperatureAlarmHysteresis1 = ByteUtil.HexToShort(data, 322, 4) / 10.0;

        rfa.pumpCurrentAlarmHysteresis2 = ByteUtil.HexToShort(data, 326, 4) / 10.0;
        rfa.pumpPowerAlarmHysteresis2 = ByteUtil.HexToShort(data, 330, 4) / 10.0;
        rfa.pumpTemperatureAlarmHysteresis2 = ByteUtil.HexToShort(data, 334, 4) / 10.0;
        rfa.reflectionAlarmThreshold = ByteUtil.HexToShort(data, 358, 4) / 10.0;
        rfa.reflexAlarmHysteresis = ByteUtil.HexToShort(data, 362, 4) / 10.0;
        rfa.reflectedPower = ByteUtil.HexToShort(data, 542, 4) / 10.0;
        rfa.ramanInputSignalPower = ByteUtil.HexToShort(data, 546, 4) / 10.0;
        rfa.ramanOutputSignalPower = ByteUtil.HexToShort(data, 550, 4) / 10.0;
        rfa.ramanPumpAPPCPowerSettingValue1 = ByteUtil.HexToShort(data, 554, 4) / 10.0;
        rfa.ramanPumpAPPCPowerSettingValue2 = ByteUtil.HexToShort(data, 558, 4) / 10.0;
        rfa.ramanInputEndFaceInterpolation = ByteUtil.HexToShort(data, 574, 4) / 10.0;

        // 告警状态4-6字节
        // rfa.alarmState1 = ByteUtil.hexString2binaryString(data.substring(338, 344));
        return rfa;
    }

    public static DeviceMachineRt parseDeviceMachineRt(String data) {
        int index = data.indexOf("EDFA");
        if(index>0){
            data = data.substring(index);
        }
 
        DeviceMachineEdfa edfa = parseDeviceMachineEdfa(data);

        DeviceMachineRt rt = new DeviceMachineRt();

        if (data.length() == 826) {
            rt.setRtType("2.5G");
            String dString = data.substring(data.length() - 92);

            dString = dString.substring(4);

            rt.setMdTemp(ByteUtil.doubleToString(ByteUtil.HexToShort(dString, 4, 4) / 256.0));
            rt.setTxBias(ByteUtil.doubleToString(ByteUtil.HexToInt(dString, 12, 4) * 2.0 / 1000.0));

            double txPower = 10 * Math.log10(ByteUtil.HexToInt(dString, 16, 4) / 10.0 / 1000.0);
            rt.setTxPower(ByteUtil.doubleToString(txPower));

            double RxPower = 10 * Math.log10(ByteUtil.HexToInt(dString, 20, 4) / 10.0 / 1000.0);
            rt.setRxPower(ByteUtil.doubleToString(RxPower));

            rt.setDdm(dString.substring(24, 28));

            rt.setLsCtWave(ByteUtil.HexToInt(dString, 32, 4) + "." + ByteUtil.HexToInt(dString, 36, 2));

            double tmpUp = ByteUtil.HexToShort(dString, 38, 4) / 256.0;
            double tmpLow = ByteUtil.HexToShort(dString, 42, 4) / 256.0;
            rt.setMdTempThs(ByteUtil.doubleToString(tmpLow) + "~" + ByteUtil.doubleToString(tmpUp));

            double TxBiasThsUp = ByteUtil.HexToInt(dString, 54, 4) * 2.0 / 1000.0;
            double TxBiasThsLow = ByteUtil.HexToInt(dString, 58, 4) * 2.0 / 1000.0;
            rt.setTxBiasThs(ByteUtil.doubleToString(TxBiasThsLow) + "~" + ByteUtil.doubleToString(TxBiasThsUp));

            double TxPowerThsUp = 10 * Math.log10(ByteUtil.HexToInt(dString, 62, 4) / 10.0 / 1000.0);
            double TxPowerThsLow = 10 * Math.log10(ByteUtil.HexToInt(dString, 66, 4) / 10.0 / 1000.0);
            rt.setTxPowerThs(ByteUtil.doubleToString(TxPowerThsLow) + "~" + ByteUtil.doubleToString(TxPowerThsUp));

            double RxPowerThsUp = 10 * Math.log10(ByteUtil.HexToInt(dString, 70, 4) / 10.0 / 1000.0);
            double RxPowerThsLow = 10 * Math.log10(ByteUtil.HexToInt(dString, 74, 4) / 10.0 / 1000.0);
            rt.setRxPowerThs(ByteUtil.doubleToString(RxPowerThsLow) + "~" + ByteUtil.doubleToString(RxPowerThsUp));

        } else {
            rt.setRtType("10G");
            String dString = data.substring(data.length() - 148);
            dString = dString.substring(4);

            rt.setDdm(dString.substring(44, 52));
            rt.setMdTemp(ByteUtil.doubleToString(ByteUtil.HexToShort(dString, 16, 4) / 256.0));
            rt.setTxBias(ByteUtil.doubleToString(ByteUtil.HexToInt(dString, 20, 4) * 2.0 / 1000.0));

            double txPower = 10 * Math.log10(ByteUtil.HexToInt(dString, 24, 4) / 10.0 / 1000.0);
            rt.setTxPower(ByteUtil.doubleToString(txPower));

            double RxPower = 10 * Math.log10(ByteUtil.HexToInt(dString, 28, 4) / 10.0 / 1000.0);
            rt.setRxPower(ByteUtil.doubleToString(RxPower));

            double tmpUp = ByteUtil.HexToShort(dString, 62 + 6, 4) / 256.0;
            double tmpLow = ByteUtil.HexToShort(dString, 66 + 6, 4) / 256.0;
            rt.setMdTempThs(ByteUtil.doubleToString(tmpLow) + "~" + ByteUtil.doubleToString(tmpUp));

            double TxBiasThsUp = ByteUtil.HexToInt(dString, 70 + 6, 4) * 2.0 / 1000.0;
            double TxBiasThsLow = ByteUtil.HexToInt(dString, 74 + 6, 4) * 2.0 / 1000.0;
            rt.setTxBiasThs(ByteUtil.doubleToString(TxBiasThsLow) + "~" + ByteUtil.doubleToString(TxBiasThsUp));

            double TxPowerThsUp = 10 * Math.log10(ByteUtil.HexToInt(dString, 78 + 6, 4) / 10.0 / 1000.0);
            double TxPowerThsLow = 10 * Math.log10(ByteUtil.HexToInt(dString, 82 + 6, 4) / 10.0 / 1000.0);
            rt.setTxPowerThs(ByteUtil.doubleToString(TxPowerThsLow) + "~" + ByteUtil.doubleToString(TxPowerThsUp));

            double RxPowerThsUp = 10 * Math.log10(ByteUtil.HexToInt(dString, 86 + 6, 4) / 10.0 / 1000.0);
            double RxPowerThsLow = 10 * Math.log10(ByteUtil.HexToInt(dString, 90 + 6, 4) / 10.0 / 1000.0);
            rt.setRxPowerThs(ByteUtil.doubleToString(RxPowerThsLow) + "~" + ByteUtil.doubleToString(RxPowerThsUp));

            rt.setLsCtWave(ByteUtil.doubleToString(ByteUtil.HexToInt(dString, 94 + 28 + 6, 4) * 0.05));
        }
        rt.setLineLocation(edfa.getLineLocation());
        rt.setAlarmState1(edfa.getAlarmState1());
        rt.setAlarmState2(edfa.getAlarmState2());
        rt.setWorkMode(edfa.getWorkMode());
        rt.setModuleType(edfa.getModuleType());

        rt.setPowerMin(edfa.getMinPower());
        rt.setPowerMax(edfa.getRatedPower());

        rt.setPumpsCount(edfa.getPumpsCount());
        rt.setLightPathSeries(edfa.getLightPathSeries() + "");
        rt.setApcPowerSetting(edfa.getApcPowerSetting());
        rt.setWorkStatus(edfa.getWorkStatus());
        rt.setInputPower(edfa.getInputPower());
        rt.setOutputPower(edfa.getOutputPower());
        rt.setTemper(edfa.getTemper());
        rt.setTemperMin(edfa.getTemperMin());
        rt.setTemperMax(edfa.getTemperMax());

        rt.setPump1Current(edfa.getPump1Current());
        rt.setPump1Power(edfa.getPump1Power());
        rt.setPump1Temper(edfa.getPump1Temper());
        rt.setRefdCurrent(edfa.getRefdCurrent());
        rt.setInputLightlessAlarmThreshold(edfa.getInputHighGate());// 错误
        rt.setInputLowGate(edfa.getInputLowGate());
        rt.setInputHighGate(edfa.getInputHighGate());
        rt.setOutputAlarmGate(edfa.getOutputAlarmGate());

        rt.setCreateTime(LocalDateTime.now());

        return rt;

    }

    public static OLP parseOLP(String data) {
        SlotData slotData = parseSlotData(data.substring(0, 102));
        data = data.substring(102);
        OLP olp = new OLP();
        olp.slotData = slotData;
        olp.workModel = data.substring(0, 2);
        olp.lineType = data.substring(2, 4);
        olp.handoverDelay = ByteUtil.HexToInt(data.substring(4, 6));
        olp.workChannel = data.substring(6, 8);
        olp.alarmInfo = data.substring(8, 10);

        return olp;
    }

    public static ElectronicTags parseElectronicTags(String data) {
        ElectronicTags electronicTag = new ElectronicTags();
        electronicTag.etegInfo = parseEtegInfo(data);
        data = data.substring(128);
        if (electronicTag.etegInfo.ver.contains("10")) {
            electronicTag.dataLen = ByteUtil.HexToInt(data.substring(0, 4));
            data = data.substring(4);
            electronicTag.edata = parseEDATA(data);
        }
        return electronicTag;
    }

    private static EDATA parseEDATA(String data) {
        EDATA edata = new EDATA();
        edata.mainframeType = data.substring(0, 6);
        edata.mainframeSn = data.substring(6, 18);
        edata.ver = data.substring(18, 20);
        edata.dataType = data.substring(20, 22);
        edata.location = data.substring(22, 22 + 64);
        edata.racksCount = ByteUtil.HexToInt(data.substring(86, 88));
        data = data.substring(88);
        List<DeviceMachineRack> rDatas = new ArrayList<>();
        List<DeviceMachineDisk> eSlotDatas = new ArrayList<>();
        for (int i = 0; i < edata.racksCount; i++) {
            DeviceMachineRack rd = parseDeviceMachineRack(data);

            data = data.substring(48);

            for (int j = 0; j < rd.getSlotNumber(); j++) {
                ElectronicTagSlotData eSlotData = parseElectronicTagSlotData(data);
                data = data.substring(134);

                eSlotDatas.add(electronicTagSlotData2Disk(eSlotData));
            }

            rDatas.add(rd);
        }
        edata.racksDatas = rDatas;
        edata.eTSlotDatas = eSlotDatas;
        return edata;
    }

    private static DeviceMachineDisk electronicTagSlotData2Disk(ElectronicTagSlotData sd) {
        DeviceMachineDisk disk = new DeviceMachineDisk();

        disk.setSlotPort(sd.getSlotPort());
        disk.setDataValidity("53");
        disk.setSlotSversion(sd.etegInfo.softwareVersion);
        disk.setSlotHversion(sd.etegInfo.hardwareVersion);
        disk.setProtocolVersion(sd.etegInfo.ver);
        disk.setModuleNum(sd.getSlotPort());
        disk.setModuleCount(9);
        disk.setModuleData("");
        disk.setTemper(ByteUtil.doubleToString(sd.etegInfo.dishTemperature));
        disk.setSlotHw(sd.etegInfo.hardwareVersion);
        disk.setLocation("");
        disk.setCreateTime(LocalDateTime.now());
        disk.setUpdateTime(LocalDateTime.now());
        disk.setDiskSn(sd.etegInfo.productMainType.substring(2, 4) + sd.etegInfo.productMainType.substring(0, 2)
                + sd.etegInfo.productMainType.substring(4, 6) + sd.etegInfo.sn.substring(2));
        return disk;
    }

    private static ElectronicTagSlotData parseElectronicTagSlotData(String data) {
        ElectronicTagSlotData eSlotData = new ElectronicTagSlotData();
        eSlotData.slotPort = ByteUtil.HexToInt(data.substring(0, 2));
        eSlotData.length = ByteUtil.HexToInt(data.substring(2, 6));
        eSlotData.etegInfo = parseEtegInfo(data.substring(6));

        return eSlotData;
    }

    public static String HexToIp(String str) {
        return ByteUtil.HexToInt(str, 0, 2) + "." + ByteUtil.HexToInt(str, 2, 2) + "." + ByteUtil.HexToInt(str, 4, 2)
                + "." + ByteUtil.HexToInt(str, 6, 2);
    }

    public static String HexToMac(String str) {
        if (str.length() < 12) {
            return "";
        }
        String mac = "";
        for (int i = 0; i < 6; i++) {
            mac += str.substring(i * 2, i * 2 + 2);
            if (i != 5) {
                mac += "-";
            }
        }
        return mac;
    }

    private static EtegInfo parseEtegInfo(String data) {
        EtegInfo etegInfo = new EtegInfo();
        etegInfo.ver = data.substring(4, 8);
        etegInfo.softwareVersion = data.substring(8, 10);
        etegInfo.hardwareVersion = data.substring(10, 12);
        etegInfo.productMainType = data.substring(12, 18);
        etegInfo.sn = data.substring(18, 26);
        etegInfo.res = data.substring(26, 30);
        if (data.length() <= 32) {
            return etegInfo;
        }
        etegInfo.communicationInterfaceType = data.substring(30, 32);
        etegInfo.ip = HexToIp(data.substring(32, 40));
        etegInfo.mask = data.substring(40, 48);
        etegInfo.gateway = data.substring(48, 56);
        etegInfo.mac = data.substring(56, 68);
        etegInfo.trapIp1 = HexToIp(data.substring(68, 76));
        etegInfo.trapIp2 = HexToIp(data.substring(76, 84));
        etegInfo.trapIp3 = HexToIp(data.substring(84, 92));
        etegInfo.udpPort = ByteUtil.HexToInt(data.substring(92, 96));
        etegInfo.trapUdp = ByteUtil.HexToInt(data.substring(96, 100));
        etegInfo.tcpPort1 = ByteUtil.HexToInt(data.substring(100, 104));
        etegInfo.tcpPort2 = ByteUtil.HexToInt(data.substring(104, 108));
        etegInfo.bootOrUser = data.substring(108, 110);
        etegInfo.dishWitdh = data.substring(110, 112);
        etegInfo.dishTemperature = ByteUtil.HexToInt(data.substring(112, 116)) / 10.0;
        etegInfo.bootVer = data.substring(116, 118);
        etegInfo.baudRate = data.substring(118, 120);
        etegInfo.resEnd = data.substring(120, 128);
        return etegInfo;
    }

    /**
     * 解析设置指令的应答
     */
    public static boolean parseResponseData(String data) {
        SetMsgResponse setMsgResponse = parseSetResponse(data);

        String cmd = setMsgResponse.cmdData.substring(0, 2);
        String cmdData = setMsgResponse.cmdData.substring(2);
        boolean result = false;
        switch (cmd) {
            case "01":
                // edfa
                if (cmdData.length() == 12) {
                    String mask = cmdData.substring(0, 2);
                    switch (mask) {
                        case "01":
                            result = cmdData.startsWith("53", 2);
                            break;
                        case "02":
                            result = cmdData.startsWith("53", 4);
                            break;
                        case "04":
                            result = cmdData.startsWith("53", 6);
                            break;
                        case "12":
                        case "18":
                            result = cmdData.startsWith("53", 10);
                            break;
                        case "0A":
                        case "10":
                            result = cmdData.startsWith("53", 8);
                            break;
                        default:
                            break;
                    }
                } else {
                    // 设置风扇应答
                    int fanCount = ByteUtil.HexToInt(cmdData, 0, 2);
                    cmdData = cmdData.substring(2);
                    // 8个风扇循环
                    for (int i = 0; i < fanCount; i++) {
                        String mask = cmdData.substring(0, 2); //
                        if (!mask.equals("00")) {
                            // result = cmdData.substring(2, 4);// 53成功
                            result = cmdData.startsWith("53", 2);
                        }
                        cmdData = cmdData.substring(4);
                    }
                }
                break;
            case "03":
                // result = cmdData.substring(0, 2);// 53成功
                result = cmdData.startsWith("53");
                break;
            case "02":
                // edfa apc功率设置应答
                if (cmdData.length() >= 10) {
                    result = cmdData.startsWith("0D0A0D0A3E");
                }

                break;
        }
        if (setMsgResponse.cmdData.startsWith("FEFE00")) {
            result = setMsgResponse.cmdData.startsWith("53", 24);
        }
        return result;
    }

    // public static void parseSetEDFAResponse(String data) {
    // int slotNum = ByteUtil.HexToInt(data, 0, 2);
    // int slotLen = ByteUtil.HexToInt(data, 2, 4);
    // String sn = data.substring(6, 18);
    // String res = data.substring(18, 22);
    // String flag = data.substring(22, 24);
    // String mask = data.substring(24, 26);

    // }

    public static SetMsgResponse parseSetResponse(String data) {

        SetMsgResponse setMsgResponse = new SetMsgResponse();

        setMsgResponse.slotNum = ByteUtil.HexToInt(data, 0, 2);
        setMsgResponse.slotLen = ByteUtil.HexToInt(data, 2, 4);
        setMsgResponse.sn = data.substring(6, 18);
        setMsgResponse.res = data.substring(18, 22);
        setMsgResponse.cmdNum = data.substring(22, 24);
        setMsgResponse.cmdLen = ByteUtil.HexToInt(data, 24, 4);
        setMsgResponse.cmdData = data.substring(28);

        return setMsgResponse;
    }

    // public static void parseSetModuleAuthResponse(String data) {
    // String command = data.substring(8, 10);
    // String resp = data.substring(10, 12);
    // }

    // public static void parseSetModuleWavelengthResponse(String data) {
    // String command = data.substring(8, 10);
    // String resp = data.substring(10, 12);
    // }

    // public static void parseSetNoLightOffMode(String data) {
    // String command = data.substring(8, 10);
    // String resp = data.substring(10, 12);
    // }

    // public static void parseSetSetFECPara(String data) {
    // String command = data.substring(8, 10);
    // String resp = data.substring(10, 12);
    // }

    public static DeviceMachineOpm paserOpm(String data, String sn) {
        DeviceMachineOpm opm = new DeviceMachineOpm();

        String deviceType = data.substring(0, 6);
        int pmType = ByteUtil.HexToInt(data, 6, 2);
        int pmPortStart = GwUtil.HexToInt(data.substring(8, 10));
        int pmPortEnd = GwUtil.HexToInt(data.substring(10, 12));
        String pmValue = data.substring(12, 12 + pmPortEnd * 8);
        opm.setDeviceType(deviceType);
        opm.setDiskSn(sn);
        opm.setPmType(pmType + "");
        opm.setPmPort(pmPortEnd + "");
        opm.setCreateTime(LocalDateTime.now());
        opm.setUpdateTime(LocalDateTime.now());

        List<DeviceMachineOpmModule> lt = new ArrayList<>();
        for (int k = pmPortStart - 1; k < pmPortEnd; k++) {
            String wavelength = pmValue.substring(k * 8, k * 8 + 2);
            int a = ByteUtil.HexToInt(pmValue.substring(k * 8 + 4, k * 8 + 4 + 4));
            double power;
            if (a < 10000) {
                power = a / 100.0;
            } else {
                power = (a - 65535) / 100.0;
            }

            DeviceMachineOpmModule m = new DeviceMachineOpmModule();
            m.setCreateTime(LocalDateTime.now());
            m.setOpmModuleNum(k + 1);
            m.setWavelength(wavelength);
            m.setLightPower(ByteUtil.doubleToString(power));

            lt.add(m);
        }
        opm.setOpmModuleList(lt);

        return opm;
    }

    public static DeviceMachineOs paserOs(String data, String sn) {
        DeviceMachineOs os = new DeviceMachineOs();

        int channelNum = ByteUtil.HexToInt(data, 0, 2);
        List<DeviceMachineOsModule> lt = new ArrayList<>();
        for (int i = 0; i < channelNum; i++) {
            String pow = data.substring(i * 4 + 2, i * 4 + 2 + 4);
            int a = ByteUtil.HexToInt(pow);
            double power = 0;
            if (a < 10000) {
                power = a / 100.0;
            } else {
                power = (a - 65535) / 100.0;
            }

            String wavelength = data.substring(channelNum * 4 + 2 + i * 2, channelNum * 4 + 2 + i * 2 + 2);

            DeviceMachineOsModule m = new DeviceMachineOsModule();
            m.setCreateTime(LocalDateTime.now());
            m.setOsModuleNum(i + 1);
            m.setWavelength(wavelength);
            m.setLightPower(ByteUtil.doubleToString(power));

            lt.add(m);
        }
        os.setLightSwitchState(ByteUtil.HexToInt(data.substring(2 + 6 * channelNum, 2 + 6 * channelNum + 2)) + "");
        os.setOsModuleList(lt);

        os.setDiskSn(sn);
        os.setChannelNum(channelNum + "");
        os.setCreateTime(LocalDateTime.now());
        os.setUpdateTime(LocalDateTime.now());

        return os;
    }

    public static DeviceMachineOsw paserOsw(String data, String sn) {
        DeviceMachineOsw osw = new DeviceMachineOsw();
        osw.setDiskSn(sn);
        osw.setHardwareVersion(ByteUtil.doubleToString(ByteUtil.HexToInt(data, 4, 2) / 10.0));
        osw.setProtocolVersion(ByteUtil.doubleToString(ByteUtil.HexToInt(data, 6, 2) / 10.0));
        osw.setOpticalSwitchType(ByteUtil.HexToInt(data, 8, 2) * 8 + "");
        if (ByteUtil.HexToInt(data, 8, 2) == 3) {
            osw.setOpticalSwitchType(4 * 8 + "");
        }

        osw.setChannelState(ByteUtil.HexToInt(data, 10, 2) + "");
        osw.setCreateTime(LocalDateTime.now());
        return osw;
    }

    public static String paserOsResponse(String data) {
        Msp2000Ter2Platform msg = DataParse.parseAllMsg(ByteUtil.hexToBytes(data));
        SetMsgResponse setMsgResponse = DataParse.parseSetResponse(msg.getData());
        String value = ByteUtil.HexToInt(setMsgResponse.cmdData.substring(26, 30)) / 100.0 + "";
        if (setMsgResponse.cmdData.substring(24, 26).equals("00")) {
            value = ByteUtil.HexToInt(setMsgResponse.cmdData.substring(26, 30)) + "";
        }

        return value;
    }

    public static DeviceMachineFms parseFmsData(String data) {
        if(StringUtils.isEmpty(data)){
            return null;
        }
        DeviceMachineFms fms = new DeviceMachineFms();
        String[] tmpArr = data.split(" ");
        if (tmpArr.length == 2) {
            String[] tmp = tmpArr[1].split(",");
            if (tmp.length == 5) {
                fms.setEndPosition(tmp[1]);
                fms.setPulseWidth(tmp[2]);
                fms.setResolution(tmp[3]);
                fms.setModel(tmp[4].substring(0, 1));
            }
        }
        fms.setCreateTime(LocalDateTime.now());
        fms.setUpdateTime(LocalDateTime.now());

        return fms;
    }

    /**
     * 解析fms 状态数据
     * 
     * @param data 数据
     * @return 状态
     */
    public static String paserfmsState(String data) {
        if(StringUtils.isEmpty(data))
            return "0";
        String status = data.substring(data.indexOf("20") + 2, data.indexOf("20") + 4);
        if (status.equals("31")) {
            return "1";
        } else {
            return "0";
        }
    }

    /**
     * 读取fms测试时间
     */
    public static String parseFmsTime(String data) {

        if (!data.contains("0D0A") && !data.contains("20")) {
            return "";
        }
        if (data.indexOf("0D0A") < data.indexOf("20")) {
            return "";
        }

        int end = data.indexOf("0D0A");
        String tmps1 = data.substring(data.indexOf("20") + 2, end);

        if (!StringUtils.isEmpty(tmps1)) {
            return ByteUtil.HexToStr(tmps1);
        } else {
            return "";
        }
    }

    /**
     * 读取fms用户光缆折射率
     */
    public static String paserfmsRefIndex(String data) {
        if (!data.contains("0D0A") && !data.contains("20")) {
            return "";
        }
        if (data.indexOf("0D0A") < data.indexOf("20")) {
            return "";
        }
        int end = data.indexOf("0D0A");
        String str = data.substring(data.indexOf("20") + 2, end);
        if (!StringUtils.isEmpty(str)) {
            return ByteUtil.HexToStr(str);
        } else {
            return "";
        }
    }

    public static String paserfmsReflectanceThreshold(String data) {
        if (!data.contains("0D0A") && !data.contains("20")) {
            return "";
        }
        if (data.indexOf("0D0A") < data.indexOf("20")) {
            return "";
        }
        data = data.replace("0D0A", "");
        String[] lt = data.split("20");

        String str = lt[lt.length - 1];
        if (!StringUtils.isEmpty(str)) {
            return ByteUtil.HexToStr(str).replace(">", "");
        } else {
            return "";
        }
    }

    public static DtsFullData paserDtsHeadData(String data) {
        int index = data.indexOf("CCAA");

        if (index < 0) {
            return null;
        }
        data = data.substring(index);

        // String sum = ByteUtil.makeChecksum(data.substring(0,data.length()-2));
        // if(!sum.equals(data.substring(data.length()-2))){
        // return null;
        // }

        DtsFullData dts = new DtsFullData();
        dts.flag1 = data.substring(0, 4);
        dts.len = ByteUtil.HexToInt(data.substring(4, 12));
        dts.adr = data.substring(12, 14);
        dts.cmd = data.substring(14, 16);
        dts.bodyData = data.substring(16, data.length() - 2);
        return dts;
    }

    /**
     * 1u路由主机解析
     * 
     * @param data
     * @return
     */
    public static ElectronicTags parse1uElectronicTags(String data) {
        ElectronicTags electronicTag = new ElectronicTags();
        electronicTag.etegInfo = parseEtegInfo(data);

        return electronicTag;
    }

    public static DeviceMachineEdfa parseDevice1UBA(String data) {
        DeviceMachineEdfa edfa = new DeviceMachineEdfa();
        String deviceSn =data.substring(data.length()-10,data.length()-2);
        edfa.setDeviceSn( deviceSn);
        int beginIndex = data.indexOf("EDFA") + 6;
        // String adr = data.substring(beginIndex,beginIndex+2);
        String sn = data.substring(beginIndex + 4, beginIndex + 10);
        String sn1 = "";
        for (int i = 0; i < 3; i++) {
            sn1 += GwUtil.padLeft((ByteUtil.HexToInt(sn, i * 2, 2) + ""), 2, '0');
        }
        edfa.setDiskSn(sn1);

        String alm1 = data.substring(beginIndex + 10, beginIndex + 12);
        String alm2 = data.substring(beginIndex + 12, beginIndex + 14);
        String alm3 = data.substring(beginIndex + 14, beginIndex + 16);
        edfa.setAlarmState1(ByteUtil.hexString2binaryString(alm1 + alm2 + alm3));

        String temp = data.substring(beginIndex + 16, beginIndex + 20);
        edfa.setTemper(ByteUtil.doubleToString(ByteUtil.HexToInt(temp) / 10.0));

        String model = data.substring(beginIndex + 20, beginIndex + 22);

        // if(model.equals("00")){
        // model="011";
        // }else
        if (model.equals("01")) {
            model = "001";// AGC
        } else if (model.equals("02")) {
            model = "010";// APC
        }
        edfa.setWorkMode(model);

        // String opPara = data.substring(beginIndex+22,beginIndex+24);

        // edfa.setApcPowerSetting(opPara);

        String pin = data.substring(beginIndex + 24, beginIndex + 28);
        edfa.setInputPower(ByteUtil.doubleToString(ByteUtil.HexToInt(pin) / 10.0 - 70));

        String pout = data.substring(beginIndex + 28, beginIndex + 32);
        edfa.setOutputPower(ByteUtil.doubleToString(ByteUtil.HexToInt(pout) / 10.0 - 70));

        String pinTh = data.substring(beginIndex + 32, beginIndex + 36);
        edfa.setInputHighGate(ByteUtil.doubleToString(ByteUtil.HexToInt(pinTh) / 10.0 - 70));

        String poutTh = data.substring(beginIndex + 36, beginIndex + 40);
        edfa.setOutputAlarmGate(ByteUtil.doubleToString(ByteUtil.HexToInt(poutTh) / 10.0 - 70));

        String p1Iop = data.substring(beginIndex + 40, beginIndex + 44);
        edfa.setPump1Current(ByteUtil.doubleToString(ByteUtil.HexToInt(p1Iop) / 10.0));
        String p1Power = data.substring(beginIndex + 44, beginIndex + 48);
        edfa.setPump1Power(ByteUtil.doubleToString(ByteUtil.HexToInt(p1Power) / 10.0));
        String p1Tchip = data.substring(beginIndex + 48, beginIndex + 52);
        edfa.setPump1Temper(ByteUtil.doubleToString(ByteUtil.HexToInt(p1Tchip) / 10.0));

        String p1Cooler = data.substring(beginIndex + 52, beginIndex + 56);
        edfa.setRefdCurrent(ByteUtil.doubleToString(ByteUtil.HexToInt(p1Cooler) / 10.0 - 1420));

        // String p2Iop= data.substring(beginIndex+56,beginIndex+60);
        // String p2Power = data.substring(beginIndex+60,beginIndex+64);
        // String p2Tchip = data.substring(beginIndex+64,beginIndex+68);
        // String p2Cooler= data.substring(beginIndex+68,beginIndex+72);

        // String pv = data.substring(beginIndex+72+28,beginIndex+72+28+4);
        // String rsn1 = data.substring(beginIndex+72+28+4,beginIndex+72+28+4+4);
        // String rsn2 = data.substring(beginIndex+72+28+8,beginIndex+72+28+8+4);
        edfa.setCreateTime(LocalDateTime.now());
        return edfa;
    }

    public static DeviceMachineRt parseDevice1URt(String data) {
        int indexOfEDFA = data.indexOf("EDFA");

        if (indexOfEDFA != -1) {
            data = data.substring(indexOfEDFA);
            System.out.println(data);
        }
        DeviceMachineEdfa edfa = parseDevice1UBA(data.substring(0,120));

        DeviceMachineRt rt = new DeviceMachineRt();
        rt.setDeviceSn(edfa.getDeviceSn());
        rt.setRtType("2.5G");
        String dString = data.substring(118);
        //不在位
        if(dString.startsWith("FFFFFF")){

        }else{

       
        rt.setLsCtWave(ByteUtil.HexToInt(dString, 0, 4) + "." + ByteUtil.HexToInt(dString, 4, 2));

        rt.setMdTemp(ByteUtil.doubleToString(ByteUtil.HexToShort(dString, 6, 4) / 256.0));

        rt.setTxBias(ByteUtil.doubleToString(ByteUtil.HexToInt(dString, 14, 4) * 2.0 / 1000.0));

        double txPower = 10 * Math.log10(ByteUtil.HexToInt(dString, 18, 4) / 10.0 / 1000.0);
        rt.setTxPower(ByteUtil.doubleToString(txPower));

        double RxPower = -40.0;
        if (ByteUtil.HexToInt(dString, 22, 4) != 0) {
            RxPower = 10 * Math.log10(ByteUtil.HexToInt(dString, 22, 4) / 10.0 / 1000.0);
        }

        rt.setRxPower(ByteUtil.doubleToString(RxPower));

        double tmpUp = ByteUtil.HexToShort(dString, 26, 4) / 256.0;
        double tmpLow = ByteUtil.HexToShort(dString, 30, 4) / 256.0;
        rt.setMdTempThs(ByteUtil.doubleToString(tmpLow) + "~" + ByteUtil.doubleToString(tmpUp));

        double TxBiasThsUp = ByteUtil.HexToInt(dString, 42, 4) * 2.0 / 1000.0;
        double TxBiasThsLow = ByteUtil.HexToInt(dString, 46, 4) * 2.0 / 1000.0;
        rt.setTxBiasThs(ByteUtil.doubleToString(TxBiasThsLow) + "~" + ByteUtil.doubleToString(TxBiasThsUp));

        double TxPowerThsUp = 10 * Math.log10(ByteUtil.HexToInt(dString, 50, 4) / 10.0 / 1000.0);
        double TxPowerThsLow = 10 * Math.log10(ByteUtil.HexToInt(dString, 54, 4) / 10.0 / 1000.0);
        rt.setTxPowerThs(ByteUtil.doubleToString(TxPowerThsLow) + "~" + ByteUtil.doubleToString(TxPowerThsUp));

        double RxPowerThsUp = 10 * Math.log10(ByteUtil.HexToInt(dString, 58, 4) / 10.0 / 1000.0);
        double RxPowerThsLow = 10 * Math.log10(ByteUtil.HexToInt(dString, 62, 4) / 10.0 / 1000.0);
        rt.setRxPowerThs(ByteUtil.doubleToString(RxPowerThsLow) + "~" + ByteUtil.doubleToString(RxPowerThsUp));

        rt.setDdm(dString.substring(66, 70));
    }
        rt.setDiskSn(edfa.getDiskSn());
        rt.setLineLocation(edfa.getLineLocation());
        rt.setAlarmState1(edfa.getAlarmState1());
        rt.setAlarmState2(edfa.getAlarmState2());
        rt.setWorkMode(edfa.getWorkMode());
        rt.setModuleType(edfa.getModuleType());

        rt.setPowerMin(edfa.getMinPower());
        rt.setPowerMax(edfa.getRatedPower());

        rt.setPumpsCount(edfa.getPumpsCount());
        rt.setLightPathSeries(edfa.getLightPathSeries() + "");
        rt.setApcPowerSetting(edfa.getApcPowerSetting());
        rt.setWorkStatus(edfa.getWorkStatus());
        rt.setInputPower(edfa.getInputPower());
        rt.setOutputPower(edfa.getOutputPower());
        rt.setTemper(edfa.getTemper());
        rt.setTemperMin(edfa.getTemperMin());
        rt.setTemperMax(edfa.getTemperMax());

        rt.setPump1Current(edfa.getPump1Current());
        rt.setPump1Power(edfa.getPump1Power());
        rt.setPump1Temper(edfa.getPump1Temper());
        rt.setRefdCurrent(edfa.getRefdCurrent());
        rt.setInputLightlessAlarmThreshold(edfa.getInputHighGate());// 错误
        rt.setInputLowGate(edfa.getInputLowGate());
        rt.setInputHighGate(edfa.getInputHighGate());
        rt.setOutputAlarmGate(edfa.getOutputAlarmGate());

        rt.setCreateTime(LocalDateTime.now());

        return rt;

    }

    public static DeviceMachineFec parseDeviceNMSFec(String data) {
        DeviceMachineFec fec = new DeviceMachineFec();
        fec.setDeviceSn( data.substring(0,8));
        LocalDateTime now = LocalDateTime.now();
        String len = data.substring(6, 8);
        String type = data.substring(8, 10);
       
        fec = parseFecAlarm(data,fec);

        if (type.equals("03")) {
            if (len.equals("A6")) {
                String fifteenMinutes1 = data.substring(34, 34 + 76 * 2);
                fec = parseFecfifteenMinutes(fifteenMinutes1, fec);
            } else {
                String clientData = data.substring(34, 34 + 36 * 2);
                String lineData = data.substring(106, 106 + 36 * 2);
                List<String> mdDatas = new ArrayList<String>();
                mdDatas.add(clientData);
                mdDatas.add(lineData);
                // String powerInfo = data.substring(178,104*2);
                String fanData = data.substring(104 * 2, 138 * 2);
                // String fanData = data.substring(178, 178 + 30 * 2);
                List<DeviceMachineFecModule> fecModules = parseFecModule(mdDatas,false);
                fec = parseFecFan(fanData,fec);
                // String loopStates = data.substring(138 * 2+8,138 * 2+8+4);
                fec.setFecModuleList(fecModules);
            }

        } else {
            
            if (len.equals("A6")) {
                String fifteenMinutes1 = data.substring(34, 34 + 76 * 2);
                fec = parseFecfifteenMinutes(fifteenMinutes1, fec);
            } else {
                String clientData = data.substring(34, 34 + 36 * 2);
                String lineData = data.substring(106, 106 + 36 * 2);
                List<String> mdDatas = new ArrayList<String>();
                mdDatas.add(clientData);
                mdDatas.add(lineData);
                
                String fanData = data.substring(178, 178 + 30 * 2);
                List<DeviceMachineFecModule> fecModules = parseFecModule(mdDatas,true);
                fec = parseFecFan(fanData,fec);
              
                fec.setFecModuleList(fecModules);
            }

        }
        fec.setCreateTime(now);
        return fec;
    }
     /**
     * 直连fec的解析
     */
    public static DeviceMachineFec parseDeviceFec(String data) {
        DeviceMachineFec fec = new DeviceMachineFec();
        LocalDateTime now = LocalDateTime.now();
      
        String type = data.substring(8, 10);
        if (type.equals("03")) {
            fec = parseFecAlarm(data,fec);

            String clientData = data.substring(34, 34 + 36 * 2);
            String lineData = data.substring(106, 106 + 36 * 2);
            List<String> mdDatas = new ArrayList<String>();
            mdDatas.add(clientData);
            mdDatas.add(lineData);
         
            List<DeviceMachineFecModule> fecModules = parseFecModule(mdDatas,false);
            // String powerInfo = data.substring(178,104*2);
            String fanData = data.substring(104 * 2, 138 * 2);
            fec = parseFecFan(fanData,fec);
            String fifteenMinutes1 = data.substring(292, 292 + 76 * 2);
            fec = parseFecfifteenMinutes(fifteenMinutes1,fec);
            fec.setCreateTime(now);
            fec.setFecModuleList(fecModules);
        } else {
            // String len = data.substring(6, 8);
            fec = parseFecAlarm(data,fec);

            String clientData = data.substring(34, 34 + 36 * 2);
            String lineData = data.substring(106, 106 + 36 * 2);
            List<String> mdDatas = new ArrayList<String>();
            mdDatas.add(clientData);
            mdDatas.add(lineData);
            List<DeviceMachineFecModule> fecModules = parseFecModule(mdDatas,true);

            String fanData = data.substring(178, 178 + 30 * 2);
           // fanData = fanData.substring(10);
            fec = parseFecFan(fanData,fec);

            // String attAdd = data.substring(238,238+8);
            // String res = data.substring(246,246+32);
            String fifteenMinutes1 = data.substring(278, 278 + 76 * 2);
            fec = parseFecfifteenMinutes(fifteenMinutes1,fec);

            fec.setCreateTime(now);
            fec.setFecModuleList(fecModules);
        }

     
        return fec;
    }

    private static DeviceMachineFec parseFecAlarm(String data,DeviceMachineFec fec){
       
        fec.setDiskSn(data.substring(12, 20));
        // String ver =
        // ByteUtil.doubleToString(ByteUtil.HexToInt(data.substring(20,22))/10.0);
        String fecOffOn = data.substring(22, 24);
        fec.setStates(ByteUtil.HexToInt(fecOffOn) + "");

        String fecClAlarm = data.substring(24, 26);
        String fecLlAlarm = data.substring(26, 28);
        fec.setAlarm(fecClAlarm + fecLlAlarm + "0000");
        return fec;
    }
    private static List<DeviceMachineFecModule> parseFecModule(List<String> mdDatas,boolean is10G){
        List<DeviceMachineFecModule> deviceMachineFecModuleList = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        if(is10G){
            for (String mdData : mdDatas) {
                DeviceMachineFecModule fm = new DeviceMachineFecModule();
    
                int index = mdData.indexOf(mdData) + 1;
                fm.setModuleName("模块" + index);
                String mdArr = mdData.substring(0, 2);
                if (mdArr.equals("FF")) {
                    fm.setStates("不在位");
                } else {
                    fm.setStates("在位");
                }
    
                String wl = mdData.substring(6, 12);
    
                double a = ByteUtil.HexToInt(wl.substring(2, 5)) + ByteUtil.HexToInt(wl.substring(5, 6)) * 0.0625;
                double b = 196.1000000 - (a - 1) * 0.05;
                double c = 299792.458 / b;
                if(c>10000){
                    c = c/10.0;
                }
                fm.setWaveLength(ByteUtil.doubleToString(c));
    
                String bc = mdData.substring(12, 18);
                fm.setBiasCurrent(ByteUtil.doubleToString(ByteUtil.HexToInt(bc) * 2 / 1000.0));
    
                String tx = mdData.substring(18, 24);
                fm.setTxPower(ByteUtil.doubleToString(10 * Math.log10(ByteUtil.HexToInt(tx) / 1000.0)));
    
                String temp = mdData.substring(24, 30);
                fm.setTemper(ByteUtil.doubleToString(ByteUtil.HexToLong(temp) / 1000.0));
    
                String rx = mdData.substring(30, 36);
                fm.setRxPower(ByteUtil.doubleToString(10 * Math.log10(ByteUtil.HexToInt(rx) / 1000.0 / 1000.0)));
    
                String alarm = mdData.substring(48, 54);
                fm.setDdm(alarm);
    
                fm.setDescription("10G");
    
                fm.setCreateTime(now);
                deviceMachineFecModuleList.add(fm);
            }
        }else{
            for (String mdData : mdDatas) {
                DeviceMachineFecModule fm = new DeviceMachineFecModule();
    
                int index = mdData.indexOf(mdData) + 1;
                fm.setModuleName("模块" + index);
    
                String wl = mdData.substring(0, 6);
                if (wl.equals("FFFFFF")) {
                    fm.setStates("不在位");
                } else {
                    fm.setStates("在位");
                }
    
                fm.setWaveLength(ByteUtil.HexToInt(wl.substring(0, 4)) + "." + ByteUtil.HexToInt(wl.substring(4, 6)));
    
                String temp = mdData.substring(6, 10);
                fm.setTemper(ByteUtil.doubleToString((Integer.valueOf(temp, 16).shortValue()) / 256.0));
    
                String bc = mdData.substring(14, 18);
                fm.setBiasCurrent(ByteUtil.doubleToString(ByteUtil.HexToInt(bc) * 2 / 1000.0));
    
                String tx = mdData.substring(18, 22);
                fm.setTxPower(ByteUtil.doubleToString(10 * Math.log10(ByteUtil.HexToInt(tx) / 10000.0)));
    
                String rx = mdData.substring(22, 26);
                double rxPower = -40;
                if (!rx.equals("0000")) {
                    rxPower = 10 * Math.log10(ByteUtil.HexToInt(rx) / 10000.0);
                }
                fm.setRxPower(ByteUtil.doubleToString(rxPower));
    
                String temperatureUpper = mdData.substring(26, 30);
                String temperatureLow = mdData.substring(30, 34);
    
                fm.setTemperMin(ByteUtil.doubleToString((Integer.valueOf(temperatureLow, 16).shortValue()) / 256.0));
                fm.setTemperMax(ByteUtil.doubleToString((Integer.valueOf(temperatureUpper, 16).shortValue()) / 256.0));
    
                String TxBiasUpper = mdData.substring(40, 44);
                String TxBiasLow = mdData.substring(44, 48);
                String TXPowerUpper = mdData.substring(48, 52);
                String TXPowerLow = mdData.substring(52, 56);
                String RxPowerUpper = mdData.substring(56, 60);
                String RxPowerLow = mdData.substring(60, 64);
    
                fm.setBiasCurrentMin(ByteUtil.doubleToString(ByteUtil.HexToInt(TxBiasUpper) * 2 / 1000.0));
                fm.setBiasCurrentMax(ByteUtil.doubleToString(ByteUtil.HexToInt(TxBiasLow) * 2 / 1000.0));
                fm.setTransmitPowerMin(
                        ByteUtil.doubleToString(10 * Math.log10(ByteUtil.HexToInt(TXPowerUpper) / 10000.0)));
                fm.setTransmitPowerMax(
                        ByteUtil.doubleToString(10 * Math.log10(ByteUtil.HexToInt(TXPowerLow) / 10000.0)));
                fm.setReceivedPowerMax(
                        ByteUtil.doubleToString(10 * Math.log10(ByteUtil.HexToInt(RxPowerUpper) / 10000.0)));
                fm.setReceivedPowerMin(
                        ByteUtil.doubleToString(10 * Math.log10(ByteUtil.HexToInt(RxPowerLow) / 10000.0)));
    
                fm.setDescription("2.5G");
               
                fm.setCreateTime(now);
                deviceMachineFecModuleList.add(fm);
            }
        }

        return deviceMachineFecModuleList;
    } 
   
   private static DeviceMachineFec parseFecFan(String fanData,DeviceMachineFec fec){

     // String attAdd = data.substring(138*2,138*2+8);
     fanData = fanData.substring(10);
     // String fanModel = fanData.substring(0,2);
     String cTemp = fanData.substring(2, 6);
     fec.setTemp(ByteUtil.doubleToString(ByteUtil.HexToInt(cTemp) / 256.0));
     // String tempHighAlarm = fanData.substring(6,10);
     // String tempLowAlarm = fanData.substring(10,14);
     // String timeControl = fanData.substring(14,26);
     String fanLifeTime = fanData.substring(26, 42);
     fec.setFanLifeTime(ByteUtil.HexToLong(fanLifeTime.substring(0, 8)) + "");
     fec.setFanRunTime(ByteUtil.HexToLong(fanLifeTime.substring(8, 16)) + "");

     String fanno = fanData.substring(42, 44);
     fec.setFanNo(ByteUtil.HexToLong(fanno) + "");
     String fanState = fanData.substring(44, 46);
     fec.setFanState(ByteUtil.HexToLong(fanState) + "");
     String fanSpeed = fanData.substring(46, 50);
     fec.setFanSpeed(ByteUtil.HexToLong(fanSpeed) + "");

        return fec;
   }
   
   private static DeviceMachineFec parseFecfifteenMinutes(String fifteenMinutes1,DeviceMachineFec fec){
      // String fifteenMinutes2 = data.substring(186,186+76*2);
      long uiRecdStartSecnd = ByteUtil.HexToLong(fifteenMinutes1.substring(8, 16));
      // long uiErrStartSecnd =
      // ByteUtil.HexToLong(f.fecData.fifteenMinutes1.substring(16, 24));
      long uiRecdEndSecnd = ByteUtil.HexToLong(fifteenMinutes1.substring(24, 32));

      fec.setRxM1NowError(ByteUtil.HexToLong(fifteenMinutes1.substring(32, 40)) + "");
      fec.setRxB2NowError(ByteUtil.HexToLong(fifteenMinutes1.substring(40, 48)) + "");
      fec.setRxB1NowError(ByteUtil.HexToLong(fifteenMinutes1.substring(48, 56)) + "");
      fec.setTxM1NowError(ByteUtil.HexToLong(fifteenMinutes1.substring(56, 64)) + "");
      fec.setTxB2NowError(ByteUtil.HexToLong(fifteenMinutes1.substring(64, 72)) + "");
      fec.setTxB1NowError(ByteUtil.HexToLong(fifteenMinutes1.substring(72, 80)) + "");

      fec.setUiBip8ErrCnt(ByteUtil.HexToLong(fifteenMinutes1.substring(80, 88)) + "");
      fec.setUiBeiErrCnt(ByteUtil.HexToLong(fifteenMinutes1.substring(88, 96)) + "");
      fec.setUi1ErrCnt(ByteUtil.HexToLong(fifteenMinutes1.substring(96, 104)) + "");
      fec.setUi0ErrCnt(ByteUtil.HexToLong(fifteenMinutes1.substring(104, 112)) + "");
      fec.setUiUncrtBlockCnt(ByteUtil.HexToLong(fifteenMinutes1.substring(112, 120)) + "");
      fec.setUiRxBlocksCnt(ByteUtil.HexToLong(fifteenMinutes1.substring(120, 128)) + "");

      fec.setUshRxB1Es(ByteUtil.HexToInt(fifteenMinutes1.substring(128, 132)) + "");
      fec.setUshRxB1Ses(ByteUtil.HexToInt(fifteenMinutes1.substring(132, 136)) + "");
      fec.setUshRxB1Uas(ByteUtil.HexToInt(fifteenMinutes1.substring(136, 140)) + "");
      fec.setUshTxB1Es(ByteUtil.HexToInt(fifteenMinutes1.substring(140, 144)) + "");
      fec.setUshTxB1Ses(ByteUtil.HexToInt(fifteenMinutes1.substring(144, 148)) + "");
      fec.setUshTxB1Uas(ByteUtil.HexToInt(fifteenMinutes1.substring(148, 152)) + "");

      fec.setUshTxB1As((uiRecdEndSecnd - uiRecdStartSecnd - Integer.parseInt(fec.getUshTxB1Uas()) + 1) + "");
      fec.setUshRxB1As((uiRecdEndSecnd - uiRecdStartSecnd - Integer.parseInt(fec.getUshRxB1Uas()) + 1) + "");

      double ufRxBer = (Double.parseDouble(fec.getUi1ErrCnt()) + Long.parseLong(fec.getUi0ErrCnt())
              + Long.parseLong(fec.getUiUncrtBlockCnt()))
              / ((Long.parseLong(fec.getUshTxB1As()) + Long.parseLong(fec.getUshRxB1As())) * 10709225316L);
      fec.setUfRxBer(ByteUtil.doubleToString(ufRxBer));
      double ubRxBer = Double.parseDouble(fec.getUiBeiErrCnt())
              / (Long.parseLong(fec.getUshTxB1As()) + Long.parseLong(fec.getUshRxB1As()) * 10709225316L);
      fec.setUbRxBer(ByteUtil.doubleToString(ubRxBer));
  
    
    return fec;
   }
   public static boolean parseSetFspOLPResponseData(String data) {
       boolean b = false;
       if(data.length()>=12 && data.substring(10,12).equals("53")){
            b = true;
       }
       return b;
   }


   
}